OGS
MeshLib::IO::VtuInterface Class Referencefinal

Detailed Description

Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures. This class is currently not inherited from Writer because VTK will implement writing to a string from 6.2 onwards.

Definition at line 29 of file VtuInterface.h.

#include <VtuInterface.h>

Collaboration diagram for MeshLib::IO::VtuInterface:
[legend]

Public Member Functions

 VtuInterface (const MeshLib::Mesh *mesh, int dataMode=vtkXMLWriter::Appended, bool compress=false)
 Provide the mesh to write and set if compression should be used.
bool writeToFile (std::filesystem::path const &file_path)
template<typename UnstructuredGridWriter>
bool writeVTU (std::string const &file_name, const int num_partitions=1, const int rank=1)

Static Public Member Functions

static MeshLib::MeshreadVTUFile (std::string const &file_name, bool const compute_element_neighbors=false)
static vtkSmartPointer< vtkUnstructuredGrid > readVtuFileToVtkUnstructuredGrid (std::string const &file_name)
static MeshLib::MeshreadVTKFile (std::string const &file_name, bool const compute_element_neighbors=false)

Private Attributes

const MeshLib::Mesh_mesh
int _data_mode
bool _use_compressor

Constructor & Destructor Documentation

◆ VtuInterface()

MeshLib::IO::VtuInterface::VtuInterface ( const MeshLib::Mesh * mesh,
int dataMode = vtkXMLWriter::Appended,
bool compress = false )
explicit

Provide the mesh to write and set if compression should be used.

Definition at line 30 of file VtuInterface.cpp.

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}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
const MeshLib::Mesh * _mesh

References _data_mode, _mesh, _use_compressor, and WARN().

Member Function Documentation

◆ readVTKFile()

MeshLib::Mesh * MeshLib::IO::VtuInterface::readVTKFile ( std::string const & file_name,
bool const compute_element_neighbors = false )
static

Read an unstructured grid from a legacy VTK file. Other data structures such as PolyData are ignored.

Returns
The converted mesh or a nullptr if reading failed

Definition at line 86 of file VtuInterface.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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.
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:23
std::string extractBaseNameWithoutExtension(std::string const &pathname)

References MeshLib::VtkMeshConverter::convertUnstructuredGrid(), ERR(), BaseLib::extractBaseNameWithoutExtension(), and BaseLib::IsFileExisting().

Referenced by anonymous_namespace{readMeshFromFile.cpp}::readMeshFromFileSerial().

◆ readVTUFile()

MeshLib::Mesh * MeshLib::IO::VtuInterface::readVTUFile ( std::string const & file_name,
bool const compute_element_neighbors = false )
static

Read an unstructured grid from a VTU file.

Returns
The converted mesh or a nullptr if reading failed

Definition at line 70 of file VtuInterface.cpp.

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}
static vtkSmartPointer< vtkUnstructuredGrid > readVtuFileToVtkUnstructuredGrid(std::string const &file_name)

References MeshLib::VtkMeshConverter::convertUnstructuredGrid(), BaseLib::extractBaseNameWithoutExtension(), and readVtuFileToVtkUnstructuredGrid().

Referenced by OGSFileConverter::convertVTU2MSH(), main(), and anonymous_namespace{readMeshFromFile.cpp}::readMeshFromFileSerial().

◆ readVtuFileToVtkUnstructuredGrid()

vtkSmartPointer< vtkUnstructuredGrid > MeshLib::IO::VtuInterface::readVtuFileToVtkUnstructuredGrid ( std::string const & file_name)
static

Definition at line 41 of file VtuInterface.cpp.

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}

References ERR(), and BaseLib::IsFileExisting().

Referenced by main(), readGrid(), and readVTUFile().

◆ writeToFile()

bool MeshLib::IO::VtuInterface::writeToFile ( std::filesystem::path const & file_path)

Writes the given mesh to file.

Returns
True on success, false on error

Definition at line 146 of file VtuInterface.cpp.

147{
148#ifdef USE_PETSC
149 BaseLib::MPI::Mpi mpi;
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}
bool writeVTU(std::string const &file_name, const int num_partitions=1, const int rank=1)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)

References MeshLib::IO::getVtuFileNameForPetscOutputWithoutExtension(), BaseLib::MPI::Mpi::rank, BaseLib::MPI::Mpi::size, and writeVTU().

Referenced by SaveMeshDialog::accept(), convert(), OGSFileConverter::convertMSH2VTU(), main(), ProcessLib::outputMeshVtk(), writeBoundary(), writeDataToMesh(), writeMeshOutput(), MeshLib::IO::writeMeshToFile(), and MeshLib::IO::writeVtu().

◆ writeVTU()

template<typename UnstructuredGridWriter>
bool MeshLib::IO::VtuInterface::writeVTU ( std::string const & file_name,
const int num_partitions = 1,
const int rank = 1 )

Writes the given mesh to vtu file.

Parameters
file_nameFile name.
num_partitionsNumber of partitions to be merged.
rankthe rank of the mpi process.
Returns
True on success, false on error

Definition at line 29 of file VtuInterface-impl.h.

32{
33 if (!_mesh)
34 {
35 ERR("VtuInterface::write(): No mesh specified.");
36 return false;
37 }
38
39#ifdef USE_PETSC
40 if (_mesh->getProperties().existsPropertyVector<unsigned char>(
41 "vtkGhostType", MeshLib::MeshItemType::Cell, 1))
42 {
43 auto* ghost_cell_property =
44 _mesh->getProperties().getPropertyVector<unsigned char>(
45 "vtkGhostType", MeshLib::MeshItemType::Cell, 1);
46 if (ghost_cell_property)
47 {
48 const_cast<MeshLib::PropertyVector<unsigned char>*>(
49 ghost_cell_property)
50 ->is_for_output = true;
51 }
52 }
53 else
54 {
55 DBUG("No vtkGhostType data in mesh '{}'.", _mesh->getName());
56 }
57#endif
58
59 vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
60 vtkSource->SetMesh(_mesh);
61
62 vtkSmartPointer<UnstructuredGridWriter> vtuWriter =
63 vtkSmartPointer<UnstructuredGridWriter>::New();
64
65 vtkSource->Update();
66 vtuWriter->SetInputData(vtkSource->GetOutput());
67
69 {
70 vtuWriter->SetCompressorTypeToZLib();
71 }
72 else
73 {
74 vtuWriter->SetCompressorTypeToNone();
75 }
76
77 vtuWriter->SetDataMode(_data_mode);
78 if (_data_mode == vtkXMLWriter::Appended)
79 {
80 vtuWriter->SetEncodeAppendedData(1);
81 }
82 if (_data_mode == vtkXMLWriter::Ascii)
83 {
84 vtkSource->Update();
85 vtkSmartPointer<vtkUnstructuredGrid> tempGrid =
86 vtkSmartPointer<vtkUnstructuredGrid>::New();
87 tempGrid->DeepCopy(vtkSource->GetOutput());
88 vtuWriter->SetInputDataObject(tempGrid);
89 }
90
91 vtuWriter->SetFileName(file_name.c_str());
92
93#ifdef USE_PETSC
94 if constexpr (std::is_same_v<UnstructuredGridWriter,
95 vtkXMLPUnstructuredGridWriter>)
96 {
97 // Set the writer controller to same communicator as OGS
98 vtkSmartPointer<vtkMPICommunicator> vtk_comm =
99 vtkSmartPointer<vtkMPICommunicator>::New();
100 MPI_Comm mpi_comm = BaseLib::MPI::OGS_COMM_WORLD;
101 vtkMPICommunicatorOpaqueComm vtk_opaque_comm(&mpi_comm);
102 vtk_comm->InitializeExternal(&vtk_opaque_comm);
103
104 vtkSmartPointer<vtkMPIController> vtk_mpi_ctrl =
105 vtkSmartPointer<vtkMPIController>::New();
106 vtk_mpi_ctrl->SetCommunicator(vtk_comm);
107
108 vtuWriter->SetController(vtk_mpi_ctrl);
109
110 vtuWriter->SetGhostLevel(1);
111 vtuWriter->SetNumberOfPieces(num_partitions);
112 vtuWriter->SetStartPiece(rank);
113 vtuWriter->SetEndPiece(rank);
114 }
115#endif
116
117#ifdef VTK_USE_64BIT_IDS
118 vtuWriter->SetHeaderTypeToUInt64();
119 // set SetIdTypeToInt64() as well?
120#endif
121
122 return (vtuWriter->Write() > 0);
123}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9

References _data_mode, _mesh, _use_compressor, MeshLib::Cell, DBUG(), ERR(), and BaseLib::MPI::OGS_COMM_WORLD.

Referenced by writeToFile().

Member Data Documentation

◆ _data_mode

int MeshLib::IO::VtuInterface::_data_mode
private

Definition at line 68 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().

◆ _mesh

const MeshLib::Mesh* MeshLib::IO::VtuInterface::_mesh
private

Definition at line 67 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().

◆ _use_compressor

bool MeshLib::IO::VtuInterface::_use_compressor
private

Definition at line 69 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().


The documentation for this class was generated from the following files: