OGS
VtuInterface.cpp
Go to the documentation of this file.
1
14#include "VtuInterface.h"
15
16#include <vtkGenericDataObjectReader.h>
17#include <vtkNew.h>
18#include <vtkXMLUnstructuredGridReader.h>
19#include <vtkXMLUnstructuredGridWriter.h>
20#if defined(USE_PETSC)
21#include <vtkXMLPUnstructuredGridWriter.h>
22#endif
23#include <vtkSmartPointer.h>
24#include <vtkUnstructuredGrid.h>
25
26#include <boost/algorithm/string/erase.hpp>
27
28#include "BaseLib/DisableFPE.h"
29#include "BaseLib/Logging.h"
30
31#ifdef USE_PETSC
32#include <petsc.h>
33#endif
34
35#include "BaseLib/FileTools.h"
36#include "MeshLib/Mesh.h"
38#include "VtkMeshConverter.h"
39
40namespace MeshLib
41{
42namespace IO
43{
44VtuInterface::VtuInterface(MeshLib::Mesh const* const mesh, int const dataMode,
45 bool const compress)
46 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
47{
48 if (_data_mode == vtkXMLWriter::Ascii && compress)
49 {
50 WARN("Ascii data cannot be compressed, ignoring compression flag.");
51 }
52}
53
54MeshLib::Mesh* VtuInterface::readVTUFile(std::string const& file_name,
55 bool const compute_element_neighbors)
56{
57 if (!BaseLib::IsFileExisting(file_name))
58 {
59 ERR("File '{:s}' does not exist.", file_name);
60 return nullptr;
61 }
62
63 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
64 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
65 reader->SetFileName(file_name.c_str());
66 {
67 // Reading the VTU files can trigger floating point exceptions. Because
68 // we are not debugging VTK (or other libraries) at this point, the
69 // floating point exceptions are temporarily disabled and are restored
70 // at the end of the scope.
71 [[maybe_unused]] DisableFPE disable_fpe;
72 reader->Update();
73 }
74
75 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
76 if (vtkGrid->GetNumberOfPoints() == 0)
77 {
78 ERR("Mesh '{:s}' contains zero points.", file_name);
79 return nullptr;
80 }
81
82 std::string const mesh_name(
85 vtkGrid, compute_element_neighbors, mesh_name);
86}
87
88MeshLib::Mesh* VtuInterface::readVTKFile(std::string const& file_name,
89 bool const compute_element_neighbors)
90{
91 if (!BaseLib::IsFileExisting(file_name))
92 {
93 ERR("File '{:s}' does not exist.", file_name);
94 return nullptr;
95 }
96
97 vtkSmartPointer<vtkGenericDataObjectReader> reader =
98 vtkSmartPointer<vtkGenericDataObjectReader>::New();
99 reader->SetFileName(file_name.c_str());
100 reader->Update();
101
102 // check for unstructured grid
103 if (reader->ReadOutputType() != 4)
104 {
105 ERR("Only VTK-files with dataset type \"Unstructured Grid\" are "
106 "currently supported.");
107 return nullptr;
108 }
109
110 reader->ReadAllFieldsOn();
111 reader->ReadAllScalarsOn();
112 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
113 if (vtkGrid->GetNumberOfPoints() == 0)
114 {
115 ERR("Mesh '{:s}' contains zero points.", file_name);
116 return nullptr;
117 }
118
119 std::string const mesh_name(
122 vtkGrid, compute_element_neighbors, mesh_name);
123}
124
125#ifdef USE_PETSC
127 std::string const& file_name)
128{
129 auto const file_name_extension = BaseLib::getFileExtension(file_name);
130 if (file_name_extension != ".vtu")
131 {
132 OGS_FATAL("Expected a .vtu file for petsc output.");
133 }
134
135 auto const file_name_base = boost::erase_last_copy(file_name, ".vtu");
136 auto basename = BaseLib::extractBaseName(file_name_base);
137
138 // Replace dots to underscores since the pvtu writing function drops all
139 // characters starting from a dot.
140 std::replace(basename.begin(), basename.end(), '.', '_');
141
142 // Restore the dirname if any.
143 auto const dirname = BaseLib::extractPath(file_name_base);
144 return BaseLib::joinPaths(dirname, basename);
145}
146#endif
147
148bool VtuInterface::writeToFile(std::filesystem::path const& file_path)
149{
150#ifdef USE_PETSC
151 int mpi_init;
152 MPI_Initialized(&mpi_init);
153 if (mpi_init == 1)
154 {
155 int mpi_size;
156 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
157 if (mpi_size == 1)
158 {
159 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
160 }
161 auto const vtu_file_name =
163 int rank;
164 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
165 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
166 mpi_size, rank);
167 }
168#endif
169 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
170}
171
172int writeVtu(MeshLib::Mesh const& mesh, std::string const& file_name,
173 int const data_mode)
174{
175 MeshLib::IO::VtuInterface writer(&mesh, data_mode);
176 auto const result = writer.writeToFile(file_name);
177 if (!result)
178 {
179 ERR("writeMeshToFile(): Could not write mesh to '{:s}'.", file_name);
180 return -1;
181 }
182 return 0;
183}
184
185} // end namespace IO
186} // end namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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.
Definition FileTools.cpp:50
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)