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>(
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}
122
123} // end namespace IO
124} // 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
std::set< std::string > _output_variable_names
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
constexpr std::string vtkGhostTypeString