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{
61 auto const name =
62 output_file.constructFilename(mesh.getName(), timestep, t, iteration);
63 pvd_file.addVTUFile(name, t);
64
65 auto const path = BaseLib::joinPaths(output_file.directory, name);
66 // Output of NaN's triggers floating point exceptions. Because we are not
67 // debugging VTK (or other libraries) at this point, the possibly set
68 // exceptions are temporarily disabled and are restored at the end of the
69 // function.
70 [[maybe_unused]] DisableFPE disable_fpe;
71 outputMeshVtk(path, mesh, output_file.compression, output_file.data_mode);
72}
73
75 std::string const& mesh_name) const
76{
77 return BaseLib::joinPaths(
79 BaseLib::constructFormattedFileName(prefix, mesh_name, 0, 0, 0) +
80 ".pvd");
81}
82
83OutputFormat::OutputFormat(std::string const& directory, std::string prefix,
84 std::string suffix, bool const compression)
85 : directory(directory),
86 prefix(std::move(prefix)),
87 suffix(std::move(suffix)),
88 compression(compression)
89{
90}
91
92std::string OutputVTKFormat::constructFilename(std::string const& mesh_name,
93 int const timestep,
94 double const t,
95 int const iteration) const
96{
97 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
98 iteration) +
99 BaseLib::constructFormattedFileName(suffix, mesh_name, timestep, t,
100 iteration) +
101 ".vtu";
102}
103
105 std::string const& mesh_name, int const timestep, double const t,
106 int const iteration) const
107{
108 return BaseLib::constructFormattedFileName(prefix, mesh_name, timestep, t,
109 iteration) +
110 ".xdmf";
111}
112
114 std::set<std::string> const& output_variables,
115 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
116 int const timestep, double const t, int const iteration) 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);
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);
126 }
127 else
128 {
129 mesh_xdmf_hdf_writer->writeStep(t);
130 };
131}
132
134 const int timestep, const double t, const int iteration,
135 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
136 [[maybe_unused]] std::set<std::string> const& output_variables) const
137{
138 for (auto const& mesh : meshes)
139 {
140 auto& pvd_file = findOrCreatePVDFile(mesh.get().getName());
141 outputMeshVtk(*this, pvd_file, mesh, t, timestep, iteration);
142 }
143}
144} // 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....
Definition: VtuInterface.h:38
bool writeToFile(std::filesystem::path const &file_path)
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:98
std::string constructFormattedFileName(std::string const &format_specification, std::string const &mesh_name, int const timestep, double const t, int const iteration)
Definition: FileTools.cpp:120
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:216
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.
Definition: OutputFormat.h:38
std::map< std::string, MeshLib::IO::PVDFile > mesh_name_to_pvd_file
Holds the PVD files associated with the meshes.
Definition: OutputFormat.h:82
void outputMeshes(const int timestep, const double t, const int iteration, std::vector< std::reference_wrapper< const MeshLib::Mesh > > const &meshes, std::set< std::string > const &output_variables) const override
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) const override
MeshLib::IO::PVDFile & findOrCreatePVDFile(std::string const &mesh_name) const
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) const
std::string constructFilename(std::string const &mesh_name, int const timestep, double const t, int const iteration) const override
unsigned int n_files
Specifies the number of hdf5 output files.
Definition: OutputFormat.h:119
std::unique_ptr< MeshLib::IO::XdmfHdfWriter > mesh_xdmf_hdf_writer
Definition: OutputFormat.h:117