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