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
50MeshLib::Mesh* VtuInterface::readVTUFile(std::string const& file_name,
51 bool const compute_element_neighbors)
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
78 std::string const mesh_name(
81 vtkGrid, compute_element_neighbors, mesh_name);
82}
83
84MeshLib::Mesh* VtuInterface::readVTKFile(std::string const& file_name,
85 bool const compute_element_neighbors)
86{
87 if (!BaseLib::IsFileExisting(file_name))
88 {
89 ERR("File '{:s}' does not exist.", file_name);
90 return nullptr;
91 }
92
93 vtkSmartPointer<vtkGenericDataObjectReader> reader =
94 vtkSmartPointer<vtkGenericDataObjectReader>::New();
95 reader->SetFileName(file_name.c_str());
96 reader->Update();
97
98 // check for unstructured grid
99 if (reader->ReadOutputType() != 4)
100 {
101 ERR("Only VTK-files with dataset type \"Unstructured Grid\" are "
102 "currently supported.");
103 return nullptr;
104 }
105
106 reader->ReadAllFieldsOn();
107 reader->ReadAllScalarsOn();
108 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
109 if (vtkGrid->GetNumberOfPoints() == 0)
110 {
111 ERR("Mesh '{:s}' contains zero points.", file_name);
112 return nullptr;
113 }
114
115 std::string const mesh_name(
118 vtkGrid, compute_element_neighbors, mesh_name);
119}
120
121#ifdef USE_PETSC
123 std::string const& file_name)
124{
125 auto const file_name_extension = BaseLib::getFileExtension(file_name);
126 if (file_name_extension != ".vtu")
127 {
128 OGS_FATAL("Expected a .vtu file for petsc output.");
129 }
130
131 auto const file_name_base = boost::erase_last_copy(file_name, ".vtu");
132 auto basename = BaseLib::extractBaseName(file_name_base);
133
134 // Replace dots to underscores since the pvtu writing function drops all
135 // characters starting from a dot.
136 std::replace(basename.begin(), basename.end(), '.', '_');
137
138 // Restore the dirname if any.
139 auto const dirname = BaseLib::extractPath(file_name_base);
140 return BaseLib::joinPaths(dirname, basename);
141}
142#endif
143
144bool VtuInterface::writeToFile(std::filesystem::path const& file_path)
145{
146#ifdef USE_PETSC
148 if (mpi.size == 1)
149 {
150 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
151 }
152 auto const vtu_file_name =
154 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
155 mpi.size, mpi.rank);
156#endif
157 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
158}
159
160int writeVtu(MeshLib::Mesh const& mesh, std::string const& file_name,
161 int const data_mode)
162{
163 MeshLib::IO::VtuInterface writer(&mesh, data_mode);
164 auto const result = writer.writeToFile(file_name);
165 if (!result)
166 {
167 ERR("writeMeshToFile(): Could not write mesh to '{:s}'.", file_name);
168 return -1;
169 }
170 return 0;
171}
172
173} // end namespace IO
174} // 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)