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 31 of file VtuInterface.h.

#include <VtuInterface.h>

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

Public Member Functions

 VtuInterface (const MeshLib::Mesh *mesh, std::set< std::string > const &output_variable_names={}, 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
std::set< std::string > _output_variable_names
int _data_mode
bool _use_compressor

Constructor & Destructor Documentation

◆ VtuInterface()

MeshLib::IO::VtuInterface::VtuInterface ( const MeshLib::Mesh * mesh,
std::set< std::string > const & output_variable_names = {},
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.

34 : _mesh(mesh),
35 _output_variable_names(output_variable_names),
36 _data_mode(dataMode),
37 _use_compressor(compress)
38{
39 if (_data_mode == vtkXMLWriter::Ascii && compress)
40 {
41 WARN("Ascii data cannot be compressed, ignoring compression flag.");
42 }
43}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::set< std::string > _output_variable_names
const MeshLib::Mesh * _mesh

References _data_mode, _mesh, _output_variable_names, _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 91 of file VtuInterface.cpp.

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

77{
78 vtkSmartPointer<vtkUnstructuredGrid> vtkGrid =
80 if (vtkGrid == nullptr)
81 {
82 return nullptr;
83 }
84
85 std::string const mesh_name(
88 vtkGrid, compute_element_neighbors, mesh_name);
89}
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 46 of file VtuInterface.cpp.

47{
48 if (!BaseLib::IsFileExisting(file_name))
49 {
50 ERR("File '{:s}' does not exist.", file_name);
51 return nullptr;
52 }
53
54 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
55 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
56 reader->SetFileName(file_name.c_str());
57 {
58 // Reading the VTU files can trigger floating point exceptions. Because
59 // we are not debugging VTK (or other libraries) at this point, the
60 // floating point exceptions are temporarily disabled and are restored
61 // at the end of the scope.
62 [[maybe_unused]] DisableFPE disable_fpe;
63 reader->Update();
64 }
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 return vtkGrid;
73}

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

152{
153#ifdef USE_PETSC
154 BaseLib::MPI::Mpi mpi;
155 if (mpi.size == 1)
156 {
157 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
158 }
159 auto const vtu_file_name =
161 return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
162 mpi.size, mpi.rank);
163#endif
164 return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
165}
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 OGSFileConverter::convertMSH2VTU(), main(), ProcessLib::outputMeshVtk(), writeBoundary(), writeDataToMesh(), and MeshLib::IO::writeMeshToFile().

◆ 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>(
42 {
45 {
47 }
48 }
49 else
50 {
51 DBUG(
52 "No '{}' data in mesh '{}'.", vtkGhostTypeString, _mesh->getName());
53 }
54#endif
55
56 vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
57 vtkSource->SetMesh(_mesh);
58 vtkSource->setOutputVariableNames(_output_variable_names);
59
60 vtkSmartPointer<UnstructuredGridWriter> vtuWriter =
61 vtkSmartPointer<UnstructuredGridWriter>::New();
62
63 vtkSource->Update();
64 vtuWriter->SetInputData(vtkSource->GetOutput());
65
67 {
68 vtuWriter->SetCompressorTypeToZLib();
69 }
70 else
71 {
72 vtuWriter->SetCompressorTypeToNone();
73 }
74
75 vtuWriter->SetDataMode(_data_mode);
76 if (_data_mode == vtkXMLWriter::Appended)
77 {
78 vtuWriter->SetEncodeAppendedData(1);
79 }
80 if (_data_mode == vtkXMLWriter::Ascii)
81 {
82 vtkSource->Update();
83 vtkSmartPointer<vtkUnstructuredGrid> tempGrid =
84 vtkSmartPointer<vtkUnstructuredGrid>::New();
85 tempGrid->DeepCopy(vtkSource->GetOutput());
86 vtuWriter->SetInputDataObject(tempGrid);
87 }
88
89 vtuWriter->SetFileName(file_name.c_str());
90
91#ifdef USE_PETSC
92 if constexpr (std::is_same_v<UnstructuredGridWriter,
93 vtkXMLPUnstructuredGridWriter>)
94 {
95 // Set the writer controller to same communicator as OGS
96 vtkSmartPointer<vtkMPICommunicator> vtk_comm =
97 vtkSmartPointer<vtkMPICommunicator>::New();
98 MPI_Comm mpi_comm = BaseLib::MPI::OGS_COMM_WORLD;
99 vtkMPICommunicatorOpaqueComm vtk_opaque_comm(&mpi_comm);
100 vtk_comm->InitializeExternal(&vtk_opaque_comm);
101
102 vtkSmartPointer<vtkMPIController> vtk_mpi_ctrl =
103 vtkSmartPointer<vtkMPIController>::New();
104 vtk_mpi_ctrl->SetCommunicator(vtk_comm);
105
106 vtuWriter->SetController(vtk_mpi_ctrl);
107
108 vtuWriter->SetGhostLevel(1);
109 vtuWriter->SetNumberOfPieces(num_partitions);
110 vtuWriter->SetStartPiece(rank);
111 vtuWriter->SetEndPiece(rank);
112 }
113#endif
114
115#ifdef VTK_USE_64BIT_IDS
116 vtuWriter->SetHeaderTypeToUInt64();
117 // set SetIdTypeToInt64() as well?
118#endif
119
120 return (vtuWriter->Write() > 0);
121}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
constexpr std::string vtkGhostTypeString

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

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 71 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().

◆ _output_variable_names

std::set<std::string> MeshLib::IO::VtuInterface::_output_variable_names
private

Definition at line 72 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().

◆ _use_compressor

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

Definition at line 74 of file VtuInterface.h.

Referenced by VtuInterface(), and writeVTU().


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