OGS
SubmeshResiduumOutputConfig.cpp
Go to the documentation of this file.
1
12
13#include <numeric>
14#include <unordered_set>
15
16#include "CreateOutput.h"
17#include "CreateOutputConfig.h"
18
19namespace
20{
21std::vector<std::reference_wrapper<MeshLib::Mesh>>
23 std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
24 std::vector<std::string> const& mesh_names_for_output)
25{
26 std::map<std::string, std::reference_wrapper<MeshLib::Mesh>>
27 map_mesh_name_to_mesh;
28 for (auto const& mesh : meshes)
29 {
30 auto const [it, inserted] =
31 map_mesh_name_to_mesh.emplace(mesh->getName(), *mesh);
32
33 if (!inserted)
34 {
35 OGS_FATAL("Duplicate mesh name '{}' detected.", mesh->getName());
36 }
37 }
38
39 std::vector<std::reference_wrapper<MeshLib::Mesh>> meshes_filtered;
40
41 for (auto const& mesh_name : mesh_names_for_output)
42 {
43 auto const& mesh = BaseLib::getOrError(
44 map_mesh_name_to_mesh, mesh_name,
45 "A mesh that has been requested for output is not known to OGS.");
46
47 meshes_filtered.push_back(mesh);
48 }
49
50 return meshes_filtered;
51}
52
53bool areElementsUnique(std::vector<std::string> const& strings)
54{
55 std::unordered_set<std::string_view> const strings_set(strings.begin(),
56 strings.end());
57
58 return strings_set.size() == strings.size();
59}
60
62{
63 auto const& properties = mesh.getProperties();
64
65 if (!properties.existsPropertyVector<std::size_t>(
68 {
70 "The required nodal property '{}' is missing in mesh '{}' or has "
71 "the wrong data type or the wrong number of components",
73 mesh.getName());
74 }
75
76 if (!properties.existsPropertyVector<std::size_t>(
79 {
81 "The required cell property '{}' is missing in mesh '{}' or has "
82 "the wrong data type or the wrong number of components",
84 mesh.getName());
85 }
86}
87
89 MeshLib::Mesh const& bulk_mesh,
90 std::vector<std::reference_wrapper<MeshLib::Mesh>> const& submesh_refs)
91{
92 auto const n_elements_bulk = bulk_mesh.getNumberOfElements();
93 auto const sum_elements_submeshes = [&submesh_refs]()
94 {
95 std::size_t n = 0;
96 for (auto const& submesh_ref : submesh_refs)
97 {
98 n += submesh_ref.get().getNumberOfElements();
99 }
100 return n;
101 }();
102
103 if (n_elements_bulk != sum_elements_submeshes)
104 {
105 OGS_FATAL(
106 "The number of bulk mesh elements does not match the sum of all "
107 "submesh elements: {} != {}. Hence, the set of all submeshes "
108 "cannot be a non-overlapping cover of the bulk mesh.",
109 n_elements_bulk, sum_elements_submeshes);
110 }
111}
112
114 MeshLib::Mesh const& bulk_mesh,
115 std::vector<std::reference_wrapper<MeshLib::Mesh>> const& submesh_refs)
116{
117 auto const n_elements_bulk = bulk_mesh.getNumberOfElements();
118
119 std::vector<bool> bulk_element_covered(n_elements_bulk);
120
121 for (auto const& submesh_ref : submesh_refs)
122 {
123 auto const& submesh = submesh_ref.get();
124 auto const& bulk_element_ids = bulkElementIDs(submesh);
125 if (bulk_element_ids == nullptr)
126 {
127 OGS_FATAL(
128 "The 'bulk_element_ids' property does not exist on the submesh "
129 "{:s}.",
130 submesh.getName());
131 }
132
133 if (bulk_element_ids->size() != submesh.getNumberOfElements())
134 {
135 OGS_FATAL(
136 "There is something terribly wrong with the mesh '{}'. The "
137 "size of 'bulk_element_ids' does not equal the number of "
138 "elements in the mesh: {} != {}",
139 submesh.getName(), bulk_element_ids->size(),
140 submesh.getNumberOfElements());
141 }
142
143 for (auto const bulk_element_id : *bulk_element_ids)
144 {
145 // meshes are provided as user input, so we better check the bounds
146 // of the contained data
147 [[unlikely]] if (bulk_element_id >= n_elements_bulk)
148 {
149 OGS_FATAL(
150 "Saw bulk element id {} in submesh '{}', but the bulk mesh "
151 "('{}') has only {} elements, i.e., the maximum allowed "
152 "bulk element id is {}.",
153 bulk_element_id, submesh.getName(), bulk_mesh.getName(),
154 n_elements_bulk, n_elements_bulk - 1);
155 }
156
157 [[unlikely]] if (bulk_element_covered[bulk_element_id])
158 {
159 OGS_FATAL(
160 "The bulk element id {} has already been covered by "
161 "another submesh. The second submesh covering this bulk "
162 "element is '{}'.",
163 bulk_element_id, submesh.getName());
164 }
165
166 bulk_element_covered[bulk_element_id] = true;
167 }
168 }
169
170 return bulk_element_covered;
171}
172
174 MeshLib::Mesh const& bulk_mesh,
175 std::vector<std::reference_wrapper<MeshLib::Mesh>> const& submesh_refs)
176{
177 checkMatchingElementCounts(bulk_mesh, submesh_refs);
178
179 auto const n_elements_bulk = bulk_mesh.getNumberOfElements();
180
181 std::vector<bool> const bulk_element_covered =
182 computeNonOverlappingBulkMeshCoverBySubmeshes(bulk_mesh, submesh_refs);
183
184 auto const n_elements_covered =
185 std::accumulate(bulk_element_covered.begin(),
186 bulk_element_covered.end(), std::size_t{0});
187
188 if (n_elements_covered == n_elements_bulk)
189 {
190 return;
191 }
192
193 // search first bulk element that has not been covered
194 auto const non_covered_it = std::find(bulk_element_covered.begin(),
195 bulk_element_covered.end(), false);
196 auto const non_covered_index =
197 std::distance(bulk_element_covered.begin(), non_covered_it);
198
199 OGS_FATAL(
200 "The bulk mesh ('{}') is not covered completely by the given "
201 "submeshes. Only {} out of {} elements are covered. The first element "
202 "that is not covered is #{}.",
203 bulk_mesh.getName(), n_elements_covered, n_elements_bulk,
204 non_covered_index);
205}
206} // namespace
207
208namespace ProcessLib
209{
211 BaseLib::ConfigTree const& config, std::string const& output_directory,
212 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
213{
214 auto oc = createOutputConfig(config, meshes);
215
216 if (oc.mesh_names_for_output.empty())
217 {
218 OGS_FATAL(
219 "You did not specify any meshes for submesh residuum output.");
220 }
221
222 if (!areElementsUnique(oc.mesh_names_for_output))
223 {
224 OGS_FATAL("The mesh names for submesh residuum output are not unique.");
225 }
226
227 auto const meshes_filtered =
228 filterMeshesForResiduumOutput(meshes, oc.mesh_names_for_output);
229
230 for (auto const& mesh : meshes_filtered)
231 {
232 checkBulkIDMappingsPresent(mesh.get());
233 }
234
235 auto const& bulk_mesh =
236 *meshes.front(); // convention: the first mesh is the bulk mesh
237 checkNonOverlappingCover(bulk_mesh, meshes_filtered);
238
239 return {createOutput(std::move(oc), output_directory, meshes),
240 std::move(meshes_filtered)};
241}
242} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
Properties & getProperties()
Definition Mesh.h:136
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:105
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
OGS_NO_DANGLING Map::mapped_type & getOrError(Map &map, Key const &key, std::string const &error_message)
Definition Algorithm.h:118
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:185
OutputConfig createOutputConfig(const BaseLib::ConfigTree &config, std::vector< std::unique_ptr< MeshLib::Mesh > > &meshes)
SubmeshResiduumOutputConfig createSubmeshResiduumOutputConfig(BaseLib::ConfigTree const &config, std::string const &output_directory, 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)
bool areElementsUnique(std::vector< std::string > const &strings)
std::vector< std::reference_wrapper< MeshLib::Mesh > > filterMeshesForResiduumOutput(std::vector< std::unique_ptr< MeshLib::Mesh > > const &meshes, std::vector< std::string > const &mesh_names_for_output)
void checkMatchingElementCounts(MeshLib::Mesh const &bulk_mesh, std::vector< std::reference_wrapper< MeshLib::Mesh > > const &submesh_refs)
std::vector< bool > computeNonOverlappingBulkMeshCoverBySubmeshes(MeshLib::Mesh const &bulk_mesh, std::vector< std::reference_wrapper< MeshLib::Mesh > > const &submesh_refs)
void checkNonOverlappingCover(MeshLib::Mesh const &bulk_mesh, std::vector< std::reference_wrapper< MeshLib::Mesh > > const &submesh_refs)