OGS
VtuInterface-impl.h
Go to the documentation of this file.
1
15#include <vtkNew.h>
16#include <vtkSmartPointer.h>
17#include <vtkUnstructuredGrid.h>
18
19#include "BaseLib/Logging.h"
20#include "MeshLib/Mesh.h"
22#include "VtuInterface.h"
23
24#ifdef USE_PETSC
25#include <mpi.h>
26#include <vtkMPI.h>
27#include <vtkMPICommunicator.h>
28#include <vtkMPIController.h>
29#endif
30
31class vtkXMLPUnstructuredGridWriter;
32
33namespace MeshLib
34{
35namespace IO
36{
37template <typename UnstructuredGridWriter>
38bool VtuInterface::writeVTU(std::string const& file_name,
39 [[maybe_unused]] const int num_partitions,
40 [[maybe_unused]] const int rank)
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}
133
134} // end namespace IO
135} // end namespace MeshLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
VtkMappedMeshSource is a source class to transform OGS meshes into complete vtkUnstructuredGrids....
Implementation of the VtuInterface class.
bool writeVTU(std::string const &file_name, const int num_partitions=1, const int rank=1)
const MeshLib::Mesh * _mesh
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