OGS
PVDFile.cpp
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 "PVDFile.h"
5
6#include <fstream>
7#include <iomanip>
8#include <limits>
9
10#ifdef USE_PETSC
11#include <mpi.h>
12
13#include "BaseLib/MPI.h"
14#endif
15
16#include "BaseLib/Error.h"
18
19namespace MeshLib
20{
21namespace IO
22{
23void PVDFile::addVTUFile(const std::string& vtu_fname, double timestep)
24{
25#ifdef USE_PETSC
26 int mpi_size;
27 MPI_Comm_size(BaseLib::MPI::OGS_COMM_WORLD, &mpi_size);
28 if (mpi_size == 1)
29 {
30 _datasets.emplace_back(timestep, vtu_fname);
31 }
32 else
33 {
34 auto const vtu_file_name =
36
37 _datasets.emplace_back(timestep, vtu_file_name + ".pvtu");
38 }
39#else
40 _datasets.emplace_back(timestep, vtu_fname);
41#endif
42
43 std::ofstream fh(pvd_filename.c_str());
44 if (!fh)
45 {
46 OGS_FATAL("could not open file `{:s}'", pvd_filename);
47 }
48
49 fh << std::setprecision(std::numeric_limits<double>::max_digits10);
50
51 fh << "<?xml version=\"1.0\"?>\n"
52 "<VTKFile type=\"Collection\" version=\"0.1\" "
53 "byte_order=\"LittleEndian\""
54 " compressor=\"vtkZLibDataCompressor\">\n"
55 " <Collection>\n";
56
57 for (auto const& pair : _datasets)
58 {
59 fh << " <DataSet timestep=\"" << pair.first
60 << "\" group=\"\" part=\"0\" file=\"" << pair.second << "\"/>\n";
61 }
62
63 fh << " </Collection>\n</VTKFile>\n";
64}
65
66} // namespace IO
67} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:19
std::string const pvd_filename
Definition PVDFile.h:29
std::vector< std::pair< double, std::string > > _datasets
Definition PVDFile.h:33
void addVTUFile(std::string const &vtu_fname, double timestep)
Add a VTU file to this PVD file.
Definition PVDFile.cpp:23
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)