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

46 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
47{
48 if (_data_mode == vtkXMLWriter::Ascii && compress)
49 {
50 WARN("Ascii data cannot be compressed, ignoring compression flag.");
51 }
52}
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 88 of file VtuInterface.cpp.

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

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

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

149{
150#ifdef USE_PETSC
151 int mpi_init;
152 MPI_Initialized(&mpi_init);
153 if (mpi_init == 1)
154 {
155 int mpi_size;
156 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
157 if (mpi_size == 1)
158 {
159 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
160 }
161 auto const vtu_file_name =
163 int rank;
164 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
165 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
166 mpi_size, rank);
167 }
168#endif
169 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
170}
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)

References MeshLib::IO::getVtuFileNameForPetscOutputWithoutExtension().

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().

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: