OGS 6.1.0-1721-g6382411ad
VtuInterface.cpp
Go to the documentation of this file.
1 
14 #include "VtuInterface.h"
15 
16 #include <vtkNew.h>
17 #include <vtkXMLUnstructuredGridReader.h>
18 #include <vtkXMLUnstructuredGridWriter.h>
19 #if defined(USE_PETSC) || defined(USE_MPI)
20 #include <vtkXMLPUnstructuredGridWriter.h>
21 #endif
22 #include <vtkSmartPointer.h>
23 #include <vtkUnstructuredGrid.h>
24 
25 #include <logog/include/logog.hpp>
26 
27 #include <boost/algorithm/string/erase.hpp>
28 
29 #ifdef USE_PETSC
30 #include <petsc.h>
31 #endif
32 
33 #include "BaseLib/FileTools.h"
34 #include "MeshLib/Mesh.h"
37 
38 namespace MeshLib
39 {
40 namespace IO
41 {
42 
43 VtuInterface::VtuInterface(const MeshLib::Mesh* mesh, int dataMode, bool compress) :
44  _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
45 {
46  if(_data_mode == vtkXMLWriter::Ascii && compress)
47  WARN("Ascii data cannot be compressed, ignoring compression flag.");
48 }
49 
50 MeshLib::Mesh* VtuInterface::readVTUFile(std::string const &file_name)
51 {
52  if (!BaseLib::IsFileExisting(file_name)) {
53  ERR("File '%s' does not exist.", file_name.c_str());
54  return nullptr;
55  }
56 
57  vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
58  vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
59  reader->SetFileName(file_name.c_str());
60  reader->Update();
61 
62  vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
63  if (vtkGrid->GetNumberOfPoints() == 0)
64  {
65  ERR("Mesh '%s' contains zero points.", file_name.c_str());
66  return nullptr;
67  }
68 
69  std::string const mesh_name (BaseLib::extractBaseNameWithoutExtension(file_name));
70  return MeshLib::VtkMeshConverter::convertUnstructuredGrid(vtkGrid, mesh_name);
71 }
72 
73 #ifdef USE_PETSC
74 std::string getVtuFileNameForPetscOutputWithoutExtension(
75  std::string const& file_name)
76 {
77  auto const file_name_extension = BaseLib::getFileExtension(file_name);
78  if (file_name_extension != "vtu")
79  {
80  OGS_FATAL("Expected a .vtu file for petsc output.");
81  }
82 
83  auto const file_name_base = boost::erase_last_copy(file_name, ".vtu");
84  auto basename = BaseLib::extractBaseName(file_name_base);
85 
86  // Replace dots to underscores since the pvtu writing function drops all
87  // characters starting from a dot.
88  std::replace(basename.begin(), basename.end(), '.', '_');
89 
90  // Restore the dirname if any.
91  auto const dirname = BaseLib::extractPath(file_name_base);
92  return BaseLib::joinPaths(dirname, basename);
93 }
94 #endif
95 
96 bool VtuInterface::writeToFile(std::string const &file_name)
97 {
98 #ifdef USE_PETSC
99  auto const vtu_file_name =
100  getVtuFileNameForPetscOutputWithoutExtension(file_name);
101  int rank;
102  MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
103  int mpi_size;
104  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
105  return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
106  mpi_size, rank);
107 #else
108  return writeVTU<vtkXMLUnstructuredGridWriter>(file_name);
109 #endif
110 }
111 } // end namespace IO
112 } // end namespace MeshLib
VtuInterface(const MeshLib::Mesh *mesh, int dataMode=vtkXMLWriter::Binary, bool compressed=false)
Provide the mesh to write and set if compression should be used.
std::string extractBaseName(std::string const &pathname)
Definition: FileTools.cpp:108
Implementation of the VtuInterface class.
Definition of the VtkMeshConverter class.
Definition of the Mesh class.
static MeshLib::Mesh * convertUnstructuredGrid(vtkUnstructuredGrid *grid, std::string const &mesh_name="vtkUnstructuredGrid")
Converts a vtkUnstructuredGrid object to a Mesh.
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists. From http://www.techbytes.ca/techbyte103.html. ...
Definition: FileTools.cpp:36
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:116
static MeshLib::Mesh * readVTUFile(std::string const &file_name)
VtkMappedMeshSource is a souce class to transform OGS meshes into complete vtkUnstructuredGrids. Usage:
Interface for heuristic search length strategy.
Definition: ProjectData.h:28
bool writeToFile(std::string const &file_name)
std::string extractPath(std::string const &pathname)
Definition: FileTools.cpp:158
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:122
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
Filename manipulation routines.
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:173