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 std::set<std::string> const& output_variables,
43 bool const compress_output, int const data_mode)
44{
45 DBUG("Writing output to '{:s}'.", file_name);
46
47 MeshLib::IO::VtuInterface vtu_interface(&mesh, output_variables, data_mode,
48 compress_output);
49 vtu_interface.writeToFile(file_name);
50}
51
52void outputMeshVtk(OutputVTKFormat const& output_file,
53 MeshLib::IO::PVDFile& pvd_file, MeshLib::Mesh const& mesh,
54 std::set<std::string> const& output_variables,
55 double const t, int const timestep, int const iteration,
56 bool const converged)
57{
58 auto const name = output_file.constructFilename(mesh.getName(), timestep, t,
59 iteration, converged);
60 pvd_file.addVTUFile(name, t);
61
62 auto const path = BaseLib::joinPaths(output_file.directory, name);
63 // Output of NaN's triggers floating point exceptions. Because we are not
64 // debugging VTK (or other libraries) at this point, the possibly set
65 // exceptions are temporarily disabled and are restored at the end of the
66 // function.
67 [[maybe_unused]] DisableFPE disable_fpe;
68 outputMeshVtk(path, mesh, output_variables, output_file.compression,
69 output_file.data_mode);
70}
71
73 std::string const& mesh_name) const
74{
75 return BaseLib::joinPaths(
77 BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0, true) +
78 ".pvd");
79}
80
81OutputFormat::OutputFormat(std::string const& directory, std::string prefix,
82 std::string suffix, bool const compression)
84 prefix(std::move(prefix)),
85 suffix(std::move(suffix)),
87{
88}
89
90std::string OutputVTKFormat::constructFilename(std::string const& mesh_name,
91 int const timestep,
92 double const t,
93 int const iteration,
94 bool const converged) const
95{
96 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
97 iteration, converged) +
98 BaseLib::constructFormattedFileName(suffix, mesh_name, timestep, t,
99 iteration, converged) +
100 ".vtu";
101}
102
104 std::string const& mesh_name, int const timestep, double const t,
105 int const iteration, bool const converged) const
106{
107 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
108 iteration, converged) +
109 ".xdmf";
110}
111
113 std::set<std::string> const& output_variables,
114 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
115 int const timestep, double const t, int const iteration,
116 bool const converged) const
117{
118 // \TODO The XdmfOutput will create on construction the XdmfHdfWriter
120 {
121 auto name = constructFilename(meshes[0].get().getName(), timestep, t,
122 iteration, converged);
123 std::filesystem::path path(BaseLib::joinPaths(directory, name));
124 mesh_xdmf_hdf_writer = std::make_unique<MeshLib::IO::XdmfHdfWriter>(
125 meshes, path, timestep, t, output_variables, compression, n_files,
127 }
128 else
129 {
130 mesh_xdmf_hdf_writer->writeStep(t);
131 };
132}
133
135 const int timestep, const double t, const int iteration,
136 bool const converged,
137 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
138 std::set<std::string> const& output_variables) const
139{
140 for (auto const& mesh : meshes)
141 {
142 auto& pvd_file = findOrCreatePVDFile(mesh.get().getName());
143 outputMeshVtk(*this, pvd_file, mesh, output_variables, t, timestep,
144 iteration, converged);
145 }
146}
147} // 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:95
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, std::set< std::string > const &output_variables, 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