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