7#include <unordered_set>
14std::vector<std::reference_wrapper<MeshLib::Mesh>>
16 std::vector<std::unique_ptr<MeshLib::Mesh>>
const& meshes,
17 std::vector<std::string>
const& mesh_names_for_output)
19 std::map<std::string, std::reference_wrapper<MeshLib::Mesh>>
20 map_mesh_name_to_mesh;
21 for (
auto const& mesh : meshes)
23 auto const [it, inserted] =
24 map_mesh_name_to_mesh.emplace(mesh->getName(), *mesh);
28 OGS_FATAL(
"Duplicate mesh name '{}' detected.", mesh->getName());
32 std::vector<std::reference_wrapper<MeshLib::Mesh>> meshes_filtered;
34 for (
auto const& mesh_name : mesh_names_for_output)
37 map_mesh_name_to_mesh, mesh_name,
38 "A mesh that has been requested for output is not known to OGS.");
40 meshes_filtered.push_back(mesh);
43 return meshes_filtered;
48 std::unordered_set<std::string_view>
const strings_set(strings.begin(),
51 return strings_set.size() == strings.size();
58 if (!properties.existsPropertyVector<std::size_t>(
63 "The required nodal property '{}' is missing in mesh '{}' or has "
64 "the wrong data type or the wrong number of components",
69 if (!properties.existsPropertyVector<std::size_t>(
74 "The required cell property '{}' is missing in mesh '{}' or has "
75 "the wrong data type or the wrong number of components",
83 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
86 auto const sum_elements_submeshes = [&submesh_refs]()
89 for (
auto const& submesh_ref : submesh_refs)
91 n += submesh_ref.get().getNumberOfElements();
96 if (n_elements_bulk != sum_elements_submeshes)
99 "The number of bulk mesh elements does not match the sum of all "
100 "submesh elements: {} != {}. Hence, the set of all submeshes "
101 "cannot be a non-overlapping cover of the bulk mesh.",
102 n_elements_bulk, sum_elements_submeshes);
108 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
112 std::vector<bool> bulk_element_covered(n_elements_bulk);
114 for (
auto const& submesh_ref : submesh_refs)
116 auto const& submesh = submesh_ref.get();
117 auto const& bulk_element_ids = bulkElementIDs(submesh);
118 if (bulk_element_ids ==
nullptr)
121 "The 'bulk_element_ids' property does not exist on the submesh "
126 if (bulk_element_ids->size() != submesh.getNumberOfElements())
129 "There is something terribly wrong with the mesh '{}'. The "
130 "size of 'bulk_element_ids' does not equal the number of "
131 "elements in the mesh: {} != {}",
132 submesh.getName(), bulk_element_ids->size(),
133 submesh.getNumberOfElements());
136 for (
auto const bulk_element_id : *bulk_element_ids)
140 [[unlikely]]
if (bulk_element_id >= n_elements_bulk)
143 "Saw bulk element id {} in submesh '{}', but the bulk mesh "
144 "('{}') has only {} elements, i.e., the maximum allowed "
145 "bulk element id is {}.",
146 bulk_element_id, submesh.getName(), bulk_mesh.
getName(),
147 n_elements_bulk, n_elements_bulk - 1);
150 [[unlikely]]
if (bulk_element_covered[bulk_element_id])
153 "The bulk element id {} has already been covered by "
154 "another submesh. The second submesh covering this bulk "
156 bulk_element_id, submesh.getName());
159 bulk_element_covered[bulk_element_id] =
true;
163 return bulk_element_covered;
168 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
174 std::vector<bool>
const bulk_element_covered =
177 auto const n_elements_covered =
178 std::accumulate(bulk_element_covered.begin(),
179 bulk_element_covered.end(), std::size_t{0});
181 if (n_elements_covered == n_elements_bulk)
187 auto const non_covered_it = std::find(bulk_element_covered.begin(),
188 bulk_element_covered.end(),
false);
189 auto const non_covered_index =
190 std::distance(bulk_element_covered.begin(), non_covered_it);
193 "The bulk mesh ('{}') is not covered completely by the given "
194 "submeshes. Only {} out of {} elements are covered. The first element "
195 "that is not covered is #{}.",
196 bulk_mesh.
getName(), n_elements_covered, n_elements_bulk,
205 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
209 if (oc.mesh_names_for_output.empty())
212 "You did not specify any meshes for submesh residuum output.");
215 if (!areElementsUnique(oc.mesh_names_for_output))
217 OGS_FATAL(
"The mesh names for submesh residuum output are not unique.");
220 auto const meshes_filtered =
221 filterMeshesForResiduumOutput(meshes, oc.mesh_names_for_output);
223 for (
auto const& mesh : meshes_filtered)
225 checkBulkIDMappingsPresent(mesh.get());
228 auto const& bulk_mesh =
230 checkNonOverlappingCover(bulk_mesh, meshes_filtered);
232 return {
createOutput(std::move(oc), output_directory, meshes),
233 std::move(meshes_filtered)};
Properties & getProperties()
const std::string getName() const
Get name of the mesh.
std::size_t getNumberOfElements() const
Get the number of elements.
OGS_NO_DANGLING Map::mapped_type & getOrError(Map &map, Key const &key, std::string const &error_message)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
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)
void checkBulkIDMappingsPresent(MeshLib::Mesh const &mesh)
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)