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