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