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 37 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 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 40 of file VtuInterface.cpp.

42 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
43{
44 if (_data_mode == vtkXMLWriter::Ascii && compress)
45 {
46 WARN("Ascii data cannot be compressed, ignoring compression flag.");
47 }
48}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
const MeshLib::Mesh * _mesh

References _data_mode, 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 84 of file VtuInterface.cpp.

86{
87 if (!BaseLib::IsFileExisting(file_name))
88 {
89 ERR("File '{:s}' does not exist.", file_name);
90 return nullptr;
91 }
92
93 vtkSmartPointer<vtkGenericDataObjectReader> reader =
94 vtkSmartPointer<vtkGenericDataObjectReader>::New();
95 reader->SetFileName(file_name.c_str());
96 reader->Update();
97
98 // check for unstructured grid
99 if (reader->ReadOutputType() != 4)
100 {
101 ERR("Only VTK-files with dataset type \"Unstructured Grid\" are "
102 "currently supported.");
103 return nullptr;
104 }
105
106 reader->ReadAllFieldsOn();
107 reader->ReadAllScalarsOn();
108 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
109 if (vtkGrid->GetNumberOfPoints() == 0)
110 {
111 ERR("Mesh '{:s}' contains zero points.", file_name);
112 return nullptr;
113 }
114
115 std::string const mesh_name(
118 vtkGrid, compute_element_neighbors, mesh_name);
119}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
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:50
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 50 of file VtuInterface.cpp.

52{
53 if (!BaseLib::IsFileExisting(file_name))
54 {
55 ERR("File '{:s}' does not exist.", file_name);
56 return nullptr;
57 }
58
59 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
60 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
61 reader->SetFileName(file_name.c_str());
62 {
63 // Reading the VTU files can trigger floating point exceptions. Because
64 // we are not debugging VTK (or other libraries) at this point, the
65 // floating point exceptions are temporarily disabled and are restored
66 // at the end of the scope.
67 [[maybe_unused]] DisableFPE disable_fpe;
68 reader->Update();
69 }
70
71 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
72 if (vtkGrid->GetNumberOfPoints() == 0)
73 {
74 ERR("Mesh '{:s}' contains zero points.", file_name);
75 return nullptr;
76 }
77
78 std::string const mesh_name(
81 vtkGrid, compute_element_neighbors, mesh_name);
82}

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

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

◆ 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 144 of file VtuInterface.cpp.

145{
146#ifdef USE_PETSC
148 if (mpi.size == 1)
149 {
150 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
151 }
152 auto const vtu_file_name =
154 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
155 mpi.size, mpi.rank);
156#endif
157 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
158}
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 38 of file VtuInterface-impl.h.

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

References _data_mode, _mesh, _use_compressor, MeshLib::Cell, DBUG(), ERR(), MeshLib::Properties::existsPropertyVector(), MeshLib::Mesh::getName(), MeshLib::Mesh::getProperties(), and MeshLib::Properties::getPropertyVector().

Referenced by writeToFile().

Member Data Documentation

◆ _data_mode

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

Definition at line 73 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().

◆ _mesh

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

Definition at line 72 of file VtuInterface.h.

Referenced by writeVTU().

◆ _use_compressor

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

Definition at line 74 of file VtuInterface.h.

Referenced by writeVTU().


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