OGS
CreateOutput.cpp
Go to the documentation of this file.
1
11#include "CreateOutput.h"
12
13#include <memory>
14#include <range/v3/algorithm/find.hpp>
15#include <tuple>
16
17#include "BaseLib/ConfigTree.h"
18#include "BaseLib/Logging.h"
19#include "BaseLib/cpp23.h"
20#include "MeshLib/Mesh.h"
22
23namespace
24{
27int convertVtkDataMode(std::string_view const& data_mode)
28{
29 using namespace std::string_view_literals;
30 constexpr std::array data_mode_lookup_table{"Ascii"sv, "Binary"sv,
31 "Appended"sv};
32 auto res = ranges::find(begin(data_mode_lookup_table),
33 end(data_mode_lookup_table), data_mode);
34 if (res == data_mode_lookup_table.end())
35 {
37 "Unsupported vtk output file data mode '{:s}'. Expected Ascii, "
38 "Binary, or Appended.",
39 data_mode);
40 }
41 return static_cast<int>(std::distance(begin(data_mode_lookup_table), res));
42}
43
44bool areOutputNamesUnique(std::vector<ProcessLib::Output> const& outputs)
45{
46 std::vector<std::string> output_names;
47 for (auto const& output : outputs)
48 {
49 auto output_mesh_names = output.getFileNamesForOutput();
50 output_names.insert(output_names.end(), output_mesh_names.begin(),
51 output_mesh_names.end());
52 }
53 std::sort(output_names.begin(), output_names.end());
54 auto const last = std::unique(output_names.begin(), output_names.end());
55 return last == output_names.end();
56}
57} // namespace
58
59namespace ProcessLib
60{
61std::unique_ptr<OutputFormat> createOutputFormat(
62 std::string const& output_directory, OutputType const output_type,
63 std::string prefix, std::string suffix, std::string const& data_mode,
64 bool const compress_output, unsigned int const number_of_files,
65 unsigned int const chunk_size_bytes)
66{
67 switch (output_type)
68 {
69 case OutputType::vtk:
70 return std::make_unique<OutputVTKFormat>(
71 output_directory, std::move(prefix), std::move(suffix),
72 compress_output, convertVtkDataMode(data_mode));
74 return std::make_unique<OutputXDMFHDF5Format>(
75 output_directory, std::move(prefix), std::move(suffix),
76 compress_output, number_of_files, chunk_size_bytes);
77 default:
79 "No supported file type provided. Read '{}' from "
80 "<output><type> in prj file. Supported: VTK, XDMF.",
81 BaseLib::to_underlying(output_type));
82 }
83}
84
85Output createOutput(OutputConfig&& oc, std::string const& output_directory,
86 std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes)
87{
88 auto output_format = createOutputFormat(
89 output_directory, oc.output_type, std::move(oc.prefix),
90 std::move(oc.suffix), oc.data_mode, oc.compress_output,
91 oc.number_of_files, oc.chunk_size_bytes);
92
93 OutputDataSpecification output_data_specification{
94 std::move(oc.output_variables), std::move(oc.fixed_output_times),
95 std::move(oc.repeats_each_steps), oc.output_extrapolation_residuals};
96
97 return {std::move(output_format), oc.output_iteration_results,
98 std::move(output_data_specification),
99 std::move(oc.mesh_names_for_output), meshes};
100}
101
102std::vector<Output> createOutput(
103 const BaseLib::ConfigTree& config,
104 std::string const& output_directory,
105 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
106{
107 std::vector<Output> outputs;
108 auto oc = createOutputConfig(config, meshes);
109 outputs.push_back(createOutput(std::move(oc), output_directory, meshes));
110 return outputs;
111}
112
113std::vector<Output> createOutputs(
114 const BaseLib::ConfigTree& output_configs,
115 std::string const& output_directory,
116 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
117{
118 DBUG("Parse outputs configuration:");
119 std::vector<Output> outputs;
120 for (auto const& output_config :
122 output_configs.getConfigSubtreeList("output"))
123 {
124 auto oc = createOutputConfig(output_config, meshes);
125 outputs.push_back(
126 createOutput(std::move(oc), output_directory, meshes));
127 }
128 if (areOutputNamesUnique(outputs))
129 {
130 return outputs;
131 }
132 else
133 {
134 OGS_FATAL(
135 "Output configuration paths are not unique. This will lead to "
136 "overwritten results or invalid / corrupted data within the "
137 "files.");
138 }
139}
140} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
constexpr auto to_underlying(E e) noexcept
Converts an enumeration to its underlying type.
Definition cpp23.h:29
OutputConfig createOutputConfig(const BaseLib::ConfigTree &config, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes)
Output createOutput(OutputConfig &&oc, std::string const &output_directory, std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes)
std::unique_ptr< OutputFormat > createOutputFormat(std::string const &output_directory, OutputType const output_type, std::string prefix, std::string suffix, std::string const &data_mode, bool const compress_output, unsigned int const number_of_files, unsigned int const chunk_size_bytes)
std::vector< Output > createOutputs(const BaseLib::ConfigTree &output_configs, std::string const &output_directory, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes)
bool areOutputNamesUnique(std::vector< ProcessLib::Output > const &outputs)
int convertVtkDataMode(std::string_view const &data_mode)
Holds information about which variables to write to output files.