OGS
VtuInterface-impl.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <vtkNew.h>
5#include <vtkSmartPointer.h>
6#include <vtkUnstructuredGrid.h>
7
8#include "BaseLib/Logging.h"
9#include "MeshLib/Mesh.h"
11#include "VtuInterface.h"
12
13#ifdef USE_PETSC
14#include <mpi.h>
15#include <vtkMPI.h>
16#include <vtkMPICommunicator.h>
17#include <vtkMPIController.h>
18
19#include "BaseLib/MPI.h"
20#endif
21
22class vtkXMLPUnstructuredGridWriter;
23
24namespace MeshLib
25{
26namespace IO
27{
28template <typename UnstructuredGridWriter>
29bool VtuInterface::writeVTU(std::string const& file_name,
30 [[maybe_unused]] const int num_partitions,
31 [[maybe_unused]] const int rank)
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>(
41 "vtkGhostType", MeshLib::MeshItemType::Cell, 1))
42 {
43 auto* ghost_cell_property =
44 _mesh->getProperties().getPropertyVector<unsigned char>(
45 "vtkGhostType", MeshLib::MeshItemType::Cell, 1);
46 if (ghost_cell_property)
47 {
49 ghost_cell_property)
50 ->is_for_output = true;
51 }
52 }
53 else
54 {
55 DBUG("No vtkGhostType data in mesh '{}'.", _mesh->getName());
56 }
57#endif
58
59 vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
60 vtkSource->SetMesh(_mesh);
61
62 vtkSmartPointer<UnstructuredGridWriter> vtuWriter =
63 vtkSmartPointer<UnstructuredGridWriter>::New();
64
65 vtkSource->Update();
66 vtuWriter->SetInputData(vtkSource->GetOutput());
67
69 {
70 vtuWriter->SetCompressorTypeToZLib();
71 }
72 else
73 {
74 vtuWriter->SetCompressorTypeToNone();
75 }
76
77 vtuWriter->SetDataMode(_data_mode);
78 if (_data_mode == vtkXMLWriter::Appended)
79 {
80 vtuWriter->SetEncodeAppendedData(1);
81 }
82 if (_data_mode == vtkXMLWriter::Ascii)
83 {
84 vtkSource->Update();
85 vtkSmartPointer<vtkUnstructuredGrid> tempGrid =
86 vtkSmartPointer<vtkUnstructuredGrid>::New();
87 tempGrid->DeepCopy(vtkSource->GetOutput());
88 vtuWriter->SetInputDataObject(tempGrid);
89 }
90
91 vtuWriter->SetFileName(file_name.c_str());
92
93#ifdef USE_PETSC
94 if constexpr (std::is_same_v<UnstructuredGridWriter,
95 vtkXMLPUnstructuredGridWriter>)
96 {
97 // Set the writer controller to same communicator as OGS
98 vtkSmartPointer<vtkMPICommunicator> vtk_comm =
99 vtkSmartPointer<vtkMPICommunicator>::New();
100 MPI_Comm mpi_comm = BaseLib::MPI::OGS_COMM_WORLD;
101 vtkMPICommunicatorOpaqueComm vtk_opaque_comm(&mpi_comm);
102 vtk_comm->InitializeExternal(&vtk_opaque_comm);
103
104 vtkSmartPointer<vtkMPIController> vtk_mpi_ctrl =
105 vtkSmartPointer<vtkMPIController>::New();
106 vtk_mpi_ctrl->SetCommunicator(vtk_comm);
107
108 vtuWriter->SetController(vtk_mpi_ctrl);
109
110 vtuWriter->SetGhostLevel(1);
111 vtuWriter->SetNumberOfPieces(num_partitions);
112 vtuWriter->SetStartPiece(rank);
113 vtuWriter->SetEndPiece(rank);
114 }
115#endif
116
117#ifdef VTK_USE_64BIT_IDS
118 vtuWriter->SetHeaderTypeToUInt64();
119 // set SetIdTypeToInt64() as well?
120#endif
121
122 return (vtuWriter->Write() > 0);
123}
124
125} // end namespace IO
126} // end namespace MeshLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
bool writeVTU(std::string const &file_name, const int num_partitions=1, const int rank=1)
const MeshLib::Mesh * _mesh
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9