OGS
OutputFormat.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 "OutputFormat.h"
5
6#include <cassert>
7#include <exception>
8#include <fstream>
9#include <vector>
10
11#include "BaseLib/DisableFPE.h"
12#include "BaseLib/FileTools.h"
16#include "ProcessLib/Process.h"
17
18namespace ProcessLib
19{
26 std::string const& mesh_name) const
27{
28 auto const it = mesh_name_to_pvd_file.find(mesh_name);
29 if (it == mesh_name_to_pvd_file.end())
30 {
31 auto const filename = constructPVDName(mesh_name);
32 auto p = mesh_name_to_pvd_file.emplace(std::piecewise_construct,
33 std::forward_as_tuple(mesh_name),
34 std::forward_as_tuple(filename));
35 return p.first->second;
36 }
37
38 return it->second;
39}
40
41void outputMeshVtk(std::string const& file_name, MeshLib::Mesh const& mesh,
42 bool const compress_output, int const data_mode)
43{
44 DBUG("Writing output to '{:s}'.", file_name);
45
46 MeshLib::IO::VtuInterface vtu_interface(&mesh, data_mode, compress_output);
47 vtu_interface.writeToFile(file_name);
48}
49
50void outputMeshVtk(OutputVTKFormat const& output_file,
51 MeshLib::IO::PVDFile& pvd_file, MeshLib::Mesh const& mesh,
52 double const t, int const timestep, int const iteration,
53 bool const converged)
54{
55 auto const name = output_file.constructFilename(mesh.getName(), timestep, t,
56 iteration, converged);
57 pvd_file.addVTUFile(name, t);
58
59 auto const path = BaseLib::joinPaths(output_file.directory, name);
60 // Output of NaN's triggers floating point exceptions. Because we are not
61 // debugging VTK (or other libraries) at this point, the possibly set
62 // exceptions are temporarily disabled and are restored at the end of the
63 // function.
64 [[maybe_unused]] DisableFPE disable_fpe;
65 outputMeshVtk(path, mesh, output_file.compression, output_file.data_mode);
66}
67
69 std::string const& mesh_name) const
70{
71 return BaseLib::joinPaths(
73 BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0, true) +
74 ".pvd");
75}
76
77OutputFormat::OutputFormat(std::string const& directory, std::string prefix,
78 std::string suffix, bool const compression)
80 prefix(std::move(prefix)),
81 suffix(std::move(suffix)),
83{
84}
85
86std::string OutputVTKFormat::constructFilename(std::string const& mesh_name,
87 int const timestep,
88 double const t,
89 int const iteration,
90 bool const converged) const
91{
92 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
93 iteration, converged) +
94 BaseLib::constructFormattedFileName(suffix, mesh_name, timestep, t,
95 iteration, converged) +
96 ".vtu";
97}
98
100 std::string const& mesh_name, int const timestep, double const t,
101 int const iteration, bool const converged) const
102{
103 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
104 iteration, converged) +
105 ".xdmf";
106}
107
109 std::set<std::string> const& output_variables,
110 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
111 int const timestep, double const t, int const iteration,
112 bool const converged) const
113{
114 // \TODO The XdmfOutput will create on construction the XdmfHdfWriter
116 {
117 auto name = constructFilename(meshes[0].get().getName(), timestep, t,
118 iteration, converged);
119 std::filesystem::path path(BaseLib::joinPaths(directory, name));
120 mesh_xdmf_hdf_writer = std::make_unique<MeshLib::IO::XdmfHdfWriter>(
121 meshes, path, timestep, t, output_variables, compression, n_files,
123 }
124 else
125 {
126 mesh_xdmf_hdf_writer->writeStep(t);
127 };
128}
129
131 const int timestep, const double t, const int iteration,
132 bool const converged,
133 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
134 [[maybe_unused]] std::set<std::string> const& output_variables) const
135{
136 for (auto const& mesh : meshes)
137 {
138 auto& pvd_file = findOrCreatePVDFile(mesh.get().getName());
139 outputMeshVtk(*this, pvd_file, mesh, t, timestep, iteration, converged);
140 }
141}
142} // namespace ProcessLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
void addVTUFile(std::string const &vtu_fname, double timestep)
Add a VTU file to this PVD file.
Definition PVDFile.cpp:23
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:94
std::string constructFormattedFileName(std::string const &format_specification, std::string const &mesh_name, int const timestep, double const t, int const iteration, bool const converged)
std::string joinPaths(std::string const &pathA, std::string const &pathB)
void outputMeshVtk(std::string const &file_name, MeshLib::Mesh const &mesh, bool const compress_output, int const data_mode)
OutputFormat(std::string const &directory, std::string prefix, std::string suffix, bool const compression)
bool compression
Enables or disables zlib-compression of the output files.
std::map< std::string, MeshLib::IO::PVDFile > mesh_name_to_pvd_file
Holds the PVD files associated with the meshes.
std::string constructPVDName(std::string const &mesh_name) const
std::string constructFilename(std::string const &mesh_name, int const timestep, double const t, int const iteration, bool const converged) const override
MeshLib::IO::PVDFile & findOrCreatePVDFile(std::string const &mesh_name) const
void outputMeshes(const int timestep, const double t, const int iteration, bool const converged, std::vector< std::reference_wrapper< const MeshLib::Mesh > > const &meshes, std::set< std::string > const &output_variables) const override
std::string constructFilename(std::string const &mesh_name, int const timestep, double const t, int const iteration, bool const converged) const override
void outputMeshXdmf(std::set< std::string > const &output_variables, std::vector< std::reference_wrapper< const MeshLib::Mesh > > const &meshes, int const timestep, double const t, int const iteration, bool const converged) const
unsigned int const chunk_size_bytes
Specifies the chunks size in bytes per hdf5 output file.
unsigned int n_files
Specifies the number of hdf5 output files.
std::unique_ptr< MeshLib::IO::XdmfHdfWriter > mesh_xdmf_hdf_writer