OGS
CreateOutputConfig.cpp
Go to the documentation of this file.
1
11#include "CreateOutputConfig.h"
12
13#include <map>
14
15#include "BaseLib/Algorithm.h"
16#include "BaseLib/ConfigTree.h"
17#include "BaseLib/StringTools.h" // required for splitMaterialIDString
18#include "MeshLib/Mesh.h"
21#ifdef USE_PETSC
23#endif // USE_PETSC
24
25std::string createMeshOutputName(std::vector<int> const& material_ids,
26 std::string const& mesh_name)
27{
28 if (material_ids.empty())
29 {
30 return mesh_name;
31 }
32 return mesh_name + "_" + fmt::format("{}", fmt::join(material_ids, "_"));
33}
34
36 BaseLib::ConfigTree const& output_mesh_config,
37 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
38{
39 auto const mesh_name = output_mesh_config.getValue<std::string>();
40 auto const& mesh = *BaseLib::findElementOrError(
41 begin(meshes), end(meshes),
42 [&mesh_name](auto const& mesh)
43 {
44 assert(mesh != nullptr);
45 return mesh->getName() == mesh_name;
46 },
47 "Required mesh with name '" + mesh_name + "' not found.");
48
49 auto material_id_string =
51 output_mesh_config.getConfigAttributeOptional<std::string>(
52 "material_ids");
53
54 if (!material_id_string)
55 {
56 return mesh_name;
57 }
58
59 auto const material_ids_for_output =
60 BaseLib::splitMaterialIdString(*material_id_string);
61#ifdef USE_PETSC
62 // this mesh isn't yet a NodePartitionedMesh
63 auto subdomain_mesh = MeshLib::createMaterialIDsBasedSubMesh(
64 mesh, material_ids_for_output,
65 createMeshOutputName(material_ids_for_output, mesh_name));
66 auto const* bulk_mesh =
67 dynamic_cast<MeshLib::NodePartitionedMesh const*>(&mesh);
69 bulk_mesh, subdomain_mesh.get()));
70#else
72 mesh, material_ids_for_output,
73 createMeshOutputName(material_ids_for_output, mesh_name)));
74#endif
75
76 return meshes.back()->getName();
77}
78
79namespace ProcessLib
80{
82 const BaseLib::ConfigTree& config,
83 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
84{
85 OutputConfig output_config;
86
87 output_config.output_type = [](auto output_type)
88 {
89 try
90 {
91 const std::map<std::string, OutputType> outputType_to_enum = {
92 {"VTK", OutputType::vtk}, {"XDMF", OutputType::xdmf}};
93 auto type = outputType_to_enum.at(output_type);
94
95 return type;
96 }
97 catch (std::out_of_range&)
98 {
100 "No supported file type provided. Read `{:s}' from <output><type> \
101 in prj File. Supported: VTK, XDMF.",
102 output_type);
103 }
105 }(config.getConfigParameter<std::string>("type"));
106
107 output_config.prefix =
109 config.getConfigParameter<std::string>("prefix", "{:meshname}");
110
111 output_config.suffix =
113 config.getConfigParameter<std::string>("suffix",
114 "_ts_{:timestep}_t_{:time}");
115
116 output_config.compress_output =
118 config.getConfigParameter("compress_output", true);
119
120 auto const hdf =
122 config.getConfigSubtreeOptional("hdf");
123
124 output_config.number_of_files = [&hdf]() -> unsigned int
125 {
126 if (hdf)
127 {
129 return hdf->getConfigParameter<unsigned int>("number_of_files");
130 }
131 return 1;
132 }();
133
134 output_config.data_mode =
136 config.getConfigParameter<std::string>("data_mode", "Appended");
137
138 // Construction of output times
139 auto& repeats_each_steps = output_config.repeats_each_steps;
140
142 if (auto const timesteps = config.getConfigSubtreeOptional("timesteps"))
143 {
145 for (auto pair : timesteps->getConfigSubtreeList("pair"))
146 {
148 auto repeat = pair.getConfigParameter<unsigned>("repeat");
150 auto each_steps = pair.getConfigParameter<unsigned>("each_steps");
151
152 assert(repeat != 0 && each_steps != 0);
153 repeats_each_steps.emplace_back(repeat, each_steps);
154 }
155
156 if (repeats_each_steps.empty())
157 {
158 OGS_FATAL(
159 "You have not given any pair (<repeat/>, <each_steps/>) that "
160 "defines at which timesteps output shall be written. "
161 "Aborting.");
162 }
163 }
164 else
165 {
166 repeats_each_steps.emplace_back(1, 1);
167 }
168
170 auto const out_vars = config.getConfigSubtree("variables");
171
172 auto& output_variables = output_config.output_variables;
173 for (auto out_var :
175 out_vars.getConfigParameterList<std::string>("variable"))
176 {
177 if (output_variables.find(out_var) != output_variables.cend())
178 {
179 OGS_FATAL("output variable `{:s}' specified more than once.",
180 out_var);
181 }
182
183 DBUG("adding output variable `{:s}'", out_var);
184 output_variables.insert(out_var);
185 }
186
187 output_config.output_extrapolation_residuals =
189 config.getConfigParameter<bool>("output_extrapolation_residuals",
190 false);
191
192 auto& mesh_names_for_output = output_config.mesh_names_for_output;
194 if (auto const meshes_config = config.getConfigSubtreeOptional("meshes"))
195 {
196 if (output_config.prefix.find("{:meshname}") == std::string::npos)
197 {
198 OGS_FATAL(
199 "There are multiple meshes defined in the output section of "
200 "the project file, but the prefix doesn't contain "
201 "'{{:meshname}}'. Thus the names for the files, the simulation "
202 "results should be written to, would not be distinguishable "
203 "for different meshes.");
204 }
206 for (auto mesh_config : meshes_config->getConfigParameterList("mesh"))
207 {
208 mesh_names_for_output.push_back(
209 parseOutputMeshConfig(mesh_config, meshes));
210 INFO("Configure mesh '{:s}' for output.",
211 mesh_names_for_output.back());
212 }
213 }
214
215 if (auto const geometrical_sets_config =
217 config.getConfigSubtreeOptional("geometrical_sets"))
218 {
219 for (
220 auto geometrical_set_config :
222 geometrical_sets_config->getConfigSubtreeList("geometrical_set"))
223 {
224 auto const geometrical_set_name =
226 geometrical_set_config.getConfigParameter<std::string>("name",
227 "");
228 auto const geometry_name =
230 geometrical_set_config.getConfigParameter<std::string>(
231 "geometry");
232 mesh_names_for_output.push_back(geometrical_set_name + "_" +
233 geometry_name);
234 }
235 }
236
237 output_config.fixed_output_times =
239 config.getConfigParameter<std::vector<double>>("fixed_output_times",
240 {});
241
242 // Remove possible duplicated elements and sort.
244
245 output_config.output_iteration_results =
247 config.getConfigParameter<bool>("output_iteration_results", false);
248
249 return output_config;
250}
251} // namespace ProcessLib
std::string createMeshOutputName(std::vector< int > const &material_ids, std::string const &mesh_name)
std::string parseOutputMeshConfig(BaseLib::ConfigTree const &output_mesh_config, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes)
#define OGS_FATAL(...)
Definition: Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:35
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:30
Definition of the Mesh class.
Definition of mesh class for partitioned mesh (by node) for parallel computing within the framework o...
Definition of string helper functions.
std::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
Definition: ConfigTree.cpp:160
T getConfigParameter(std::string const &param) const
ConfigTree getConfigSubtree(std::string const &root) const
Definition: ConfigTree.cpp:151
std::optional< T > getConfigAttributeOptional(std::string const &attr) const
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:99
std::iterator_traits< InputIt >::reference findElementOrError(InputIt begin, InputIt end, Predicate predicate, std::string const &error="")
Definition: Algorithm.h:69
std::vector< int > splitMaterialIdString(std::string const &material_id_string)
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:165
std::unique_ptr< MeshLib::Mesh > createMaterialIDsBasedSubMesh(MeshLib::Mesh const &mesh, std::vector< int > const &material_ids, std::string const &name_for_created_mesh)
std::unique_ptr< NodePartitionedMesh > transformMeshToNodePartitionedMesh(NodePartitionedMesh const *const bulk_mesh, Mesh const *const subdomain_mesh)
OutputConfig createOutputConfig(const BaseLib::ConfigTree &config, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes)
std::vector< PairRepeatEachSteps > repeats_each_steps
Definition: OutputConfig.h:35
std::vector< double > fixed_output_times
Definition: OutputConfig.h:39
unsigned int number_of_files
Definition: OutputConfig.h:33
std::set< std::string > output_variables
Definition: OutputConfig.h:36
std::vector< std::string > mesh_names_for_output
Definition: OutputConfig.h:38