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>
46 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
48 if (
_data_mode == vtkXMLWriter::Ascii && compress)
50 WARN(
"Ascii data cannot be compressed, ignoring compression flag.");
55 bool const compute_element_neighbors)
59 ERR(
"File '{:s}' does not exist.", file_name);
63 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
64 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
65 reader->SetFileName(file_name.c_str());
75 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
76 if (vtkGrid->GetNumberOfPoints() == 0)
78 ERR(
"Mesh '{:s}' contains zero points.", file_name);
82 std::string
const mesh_name(
85 vtkGrid, compute_element_neighbors, mesh_name);
89 bool const compute_element_neighbors)
93 ERR(
"File '{:s}' does not exist.", file_name);
97 vtkSmartPointer<vtkGenericDataObjectReader> reader =
98 vtkSmartPointer<vtkGenericDataObjectReader>::New();
99 reader->SetFileName(file_name.c_str());
103 if (reader->ReadOutputType() != 4)
105 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
106 "currently supported.");
110 reader->ReadAllFieldsOn();
111 reader->ReadAllScalarsOn();
112 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
113 if (vtkGrid->GetNumberOfPoints() == 0)
115 ERR(
"Mesh '{:s}' contains zero points.", file_name);
119 std::string
const mesh_name(
122 vtkGrid, compute_element_neighbors, mesh_name);
127 std::string
const& file_name)
130 if (file_name_extension !=
".vtu")
132 OGS_FATAL(
"Expected a .vtu file for petsc output.");
135 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
140 std::replace(basename.begin(), basename.end(),
'.',
'_');
152 MPI_Initialized(&mpi_init);
156 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
161 auto const vtu_file_name =
164 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
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)
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)