OGS
OutputFormat.cpp
Go to the documentation of this file.
1
11#include "OutputFormat.h"
12
13#include <cassert>
14#include <exception>
15#include <fstream>
16#include <vector>
17
18#include "BaseLib/DisableFPE.h"
19#include "BaseLib/FileTools.h"
23#include "ProcessLib/Process.h"
24
25namespace ProcessLib
26{
33 std::string const& mesh_name) const
34{
35 auto const it = mesh_name_to_pvd_file.find(mesh_name);
36 if (it == mesh_name_to_pvd_file.end())
37 {
38 auto const filename = constructPVDName(mesh_name);
39 auto p = mesh_name_to_pvd_file.emplace(std::piecewise_construct,
40 std::forward_as_tuple(mesh_name),
41 std::forward_as_tuple(filename));
42 return p.first->second;
43 }
44
45 return it->second;
46}
47
48void outputMeshVtk(std::string const& file_name, MeshLib::Mesh const& mesh,
49 bool const compress_output, int const data_mode)
50{
51 DBUG("Writing output to '{:s}'.", file_name);
52
53 MeshLib::IO::VtuInterface vtu_interface(&mesh, data_mode, compress_output);
54 vtu_interface.writeToFile(file_name);
55}
56
57void outputMeshVtk(OutputVTKFormat const& output_file,
58 MeshLib::IO::PVDFile& pvd_file, MeshLib::Mesh const& mesh,
59 double const t, int const timestep, int const iteration,
60 bool const converged)
61{
62 auto const name = output_file.constructFilename(mesh.getName(), timestep, t,
63 iteration, converged);
64 pvd_file.addVTUFile(name, t);
65
66 auto const path = BaseLib::joinPaths(output_file.directory, name);
67 // Output of NaN's triggers floating point exceptions. Because we are not
68 // debugging VTK (or other libraries) at this point, the possibly set
69 // exceptions are temporarily disabled and are restored at the end of the
70 // function.
71 [[maybe_unused]] DisableFPE disable_fpe;
72 outputMeshVtk(path, mesh, output_file.compression, output_file.data_mode);
73}
74
76 std::string const& mesh_name) const
77{
78 return BaseLib::joinPaths(
80 BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0, true) +
81 ".pvd");
82}
83
84OutputFormat::OutputFormat(std::string const& directory, std::string prefix,
85 std::string suffix, bool const compression)
86 : directory(directory),
87 prefix(std::move(prefix)),
88 suffix(std::move(suffix)),
89 compression(compression)
90{
91}
92
93std::string OutputVTKFormat::constructFilename(std::string const& mesh_name,
94 int const timestep,
95 double const t,
96 int const iteration,
97 bool const converged) const
98{
99 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
100 iteration, converged) +
101 BaseLib::constructFormattedFileName(suffix, mesh_name, timestep, t,
102 iteration, converged) +
103 ".vtu";
104}
105
107 std::string const& mesh_name, int const timestep, double const t,
108 int const iteration, bool const converged) const
109{
110 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
111 iteration, converged) +
112 ".xdmf";
113}
114
116 std::set<std::string> const& output_variables,
117 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
118 int const timestep, double const t, int const iteration,
119 bool const converged) const
120{
121 // \TODO The XdmfOutput will create on construction the XdmfHdfWriter
123 {
124 auto name = constructFilename(meshes[0].get().getName(), timestep, t,
125 iteration, converged);
126 std::filesystem::path path(BaseLib::joinPaths(directory, name));
127 mesh_xdmf_hdf_writer = std::make_unique<MeshLib::IO::XdmfHdfWriter>(
128 meshes, path, timestep, t, output_variables, compression, n_files,
130 }
131 else
132 {
133 mesh_xdmf_hdf_writer->writeStep(t);
134 };
135}
136
138 const int timestep, const double t, const int iteration,
139 bool const converged,
140 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
141 [[maybe_unused]] std::set<std::string> const& output_variables) const
142{
143 for (auto const& mesh : meshes)
144 {
145 auto& pvd_file = findOrCreatePVDFile(mesh.get().getName());
146 outputMeshVtk(*this, pvd_file, mesh, t, timestep, iteration, converged);
147 }
148}
149} // namespace ProcessLib
Filename manipulation routines.
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
Implementation of the VtuInterface class.
XdmfWriter which create contiguous data for geometry and topology and writes this and all attributes ...
void addVTUFile(std::string const &vtu_fname, double timestep)
Add a VTU file to this PVD file.
Definition PVDFile.cpp:28
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:105
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