16 #include <vtkGenericDataObjectReader.h>
18 #include <vtkXMLUnstructuredGridReader.h>
19 #include <vtkXMLUnstructuredGridWriter.h>
20 #if defined(USE_PETSC)
21 #include <vtkXMLPUnstructuredGridWriter.h>
23 #include <vtkSmartPointer.h>
24 #include <vtkUnstructuredGrid.h>
26 #include <boost/algorithm/string/erase.hpp>
45 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
47 if (
_data_mode == vtkXMLWriter::Ascii && compress)
49 WARN(
"Ascii data cannot be compressed, ignoring compression flag.");
57 ERR(
"File '{:s}' does not exist.", file_name);
61 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
62 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
63 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);
73 std::string
const mesh_name(
83 ERR(
"File '{:s}' does not exist.", file_name);
87 vtkSmartPointer<vtkGenericDataObjectReader> reader =
88 vtkSmartPointer<vtkGenericDataObjectReader>::New();
89 reader->SetFileName(file_name.c_str());
93 if (reader->ReadOutputType() != 4)
95 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
96 "currently supported.");
100 reader->ReadAllFieldsOn();
101 reader->ReadAllScalarsOn();
102 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
103 if (vtkGrid->GetNumberOfPoints() == 0)
105 ERR(
"Mesh '{:s}' contains zero points.", file_name);
109 std::string
const mesh_name(
117 std::string
const& file_name)
120 if (file_name_extension !=
".vtu")
122 OGS_FATAL(
"Expected a .vtu file for petsc output.");
125 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
130 std::replace(basename.begin(), basename.end(),
'.',
'_');
141 auto const vtu_file_name =
144 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
146 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
147 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name +
".pvtu",
150 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
161 ERR(
"writeMeshToFile(): Could not write mesh to '{:s}'.", file_name);
void ERR(char const *fmt, Args const &... args)
void WARN(char const *fmt, Args const &... 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....
static MeshLib::Mesh * readVTKFile(std::string const &file_name)
static MeshLib::Mesh * readVTUFile(std::string const &file_name)
VtuInterface(const MeshLib::Mesh *mesh, int dataMode=vtkXMLWriter::Appended, bool compressed=false)
Provide the mesh to write and set if compression should be used.
bool writeToFile(std::filesystem::path const &file_path)
static MeshLib::Mesh * convertUnstructuredGrid(vtkUnstructuredGrid *grid, 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)