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/Logging.h"
29 
30 #ifdef USE_PETSC
31 #include <petsc.h>
32 #endif
33 
34 #include "BaseLib/FileTools.h"
35 #include "MeshLib/Mesh.h"
38 
39 namespace MeshLib
40 {
41 namespace IO
42 {
43 VtuInterface::VtuInterface(const MeshLib::Mesh* mesh, int dataMode,
44  bool compress)
45  : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
46 {
47  if (_data_mode == vtkXMLWriter::Ascii && compress)
48  {
49  WARN("Ascii data cannot be compressed, ignoring compression flag.");
50  }
51 }
52 
53 MeshLib::Mesh* VtuInterface::readVTUFile(std::string const& file_name)
54 {
55  if (!BaseLib::IsFileExisting(file_name))
56  {
57  ERR("File '{:s}' does not exist.", file_name);
58  return nullptr;
59  }
60 
61  vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
62  vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
63  reader->SetFileName(file_name.c_str());
64  reader->Update();
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 
73  std::string const mesh_name(
76  mesh_name);
77 }
78 
79 MeshLib::Mesh* VtuInterface::readVTKFile(std::string const& file_name)
80 {
81  if (!BaseLib::IsFileExisting(file_name))
82  {
83  ERR("File '{:s}' does not exist.", file_name);
84  return nullptr;
85  }
86 
87  vtkSmartPointer<vtkGenericDataObjectReader> reader =
88  vtkSmartPointer<vtkGenericDataObjectReader>::New();
89  reader->SetFileName(file_name.c_str());
90  reader->Update();
91 
92  // check for unstructured grid
93  if (reader->ReadOutputType() != 4)
94  {
95  ERR("Only VTK-files with dataset type \"Unstructured Grid\" are "
96  "currently supported.");
97  return nullptr;
98  }
99 
100  reader->ReadAllFieldsOn();
101  reader->ReadAllScalarsOn();
102  vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
103  if (vtkGrid->GetNumberOfPoints() == 0)
104  {
105  ERR("Mesh '{:s}' contains zero points.", file_name);
106  return nullptr;
107  }
108 
109  std::string const mesh_name(
112  mesh_name);
113 }
114 
115 #ifdef USE_PETSC
117  std::string const& file_name)
118 {
119  auto const file_name_extension = BaseLib::getFileExtension(file_name);
120  if (file_name_extension != ".vtu")
121  {
122  OGS_FATAL("Expected a .vtu file for petsc output.");
123  }
124 
125  auto const file_name_base = boost::erase_last_copy(file_name, ".vtu");
126  auto basename = BaseLib::extractBaseName(file_name_base);
127 
128  // Replace dots to underscores since the pvtu writing function drops all
129  // characters starting from a dot.
130  std::replace(basename.begin(), basename.end(), '.', '_');
131 
132  // Restore the dirname if any.
133  auto const dirname = BaseLib::extractPath(file_name_base);
134  return BaseLib::joinPaths(dirname, basename);
135 }
136 #endif
137 
138 bool VtuInterface::writeToFile(std::filesystem::path const& file_path)
139 {
140 #ifdef USE_PETSC
141  auto const vtu_file_name =
143  int rank;
144  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
145  int mpi_size;
146  MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
147  return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
148  mpi_size, rank);
149 #else
150  return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
151 #endif
152 }
153 
154 int writeVtu(MeshLib::Mesh const& mesh, std::string const& file_name,
155  int const data_mode)
156 {
157  MeshLib::IO::VtuInterface writer(&mesh, data_mode);
158  auto const result = writer.writeToFile(file_name);
159  if (!result)
160  {
161  ERR("writeMeshToFile(): Could not write mesh to '{:s}'.", file_name);
162  return -1;
163  }
164  return 0;
165 }
166 
167 } // end namespace IO
168 } // end namespace MeshLib
#define OGS_FATAL(...)
Definition: Error.h:26
Filename manipulation routines.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
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....
Definition: VtuInterface.h:38
static MeshLib::Mesh * readVTKFile(std::string const &file_name)
static MeshLib::Mesh * readVTUFile(std::string const &file_name)
VtuInterface(const MeshLib::Mesh *mesh, int dataMode=vtkXMLWriter::Appended, bool compressed=false)
Provide the mesh to write and set if compression should be used.
bool writeToFile(std::filesystem::path const &file_path)
static MeshLib::Mesh * convertUnstructuredGrid(vtkUnstructuredGrid *grid, std::string const &mesh_name="vtkUnstructuredGrid")
Converts a vtkUnstructuredGrid object to a Mesh.
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:186
std::string extractPath(std::string const &pathname)
Definition: FileTools.cpp:207
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition: FileTools.cpp:43
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:180
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:212
std::string extractBaseName(std::string const &pathname)
Definition: FileTools.cpp:175
int writeVtu(MeshLib::Mesh const &mesh, std::string const &file_name, int const data_mode)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)