14#include <unordered_set>
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)
26 std::map<std::string, std::reference_wrapper<MeshLib::Mesh>>
27 map_mesh_name_to_mesh;
28 for (
auto const& mesh : meshes)
30 auto const [it, inserted] =
31 map_mesh_name_to_mesh.emplace(mesh->getName(), *mesh);
35 OGS_FATAL(
"Duplicate mesh name '{}' detected.", mesh->getName());
39 std::vector<std::reference_wrapper<MeshLib::Mesh>> meshes_filtered;
41 for (
auto const& mesh_name : mesh_names_for_output)
44 map_mesh_name_to_mesh, mesh_name,
45 "A mesh that has been requested for output is not known to OGS.");
47 meshes_filtered.push_back(mesh);
50 return meshes_filtered;
55 std::unordered_set<std::string_view>
const strings_set(strings.begin(),
58 return strings_set.size() == strings.size();
65 if (!properties.existsPropertyVector<std::size_t>(
70 "The required nodal property '{}' is missing in mesh '{}' or has "
71 "the wrong data type or the wrong number of components",
76 if (!properties.existsPropertyVector<std::size_t>(
81 "The required cell property '{}' is missing in mesh '{}' or has "
82 "the wrong data type or the wrong number of components",
90 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
93 auto const sum_elements_submeshes = [&submesh_refs]()
96 for (
auto const& submesh_ref : submesh_refs)
98 n += submesh_ref.get().getNumberOfElements();
103 if (n_elements_bulk != sum_elements_submeshes)
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);
115 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
119 std::vector<bool> bulk_element_covered(n_elements_bulk);
121 for (
auto const& submesh_ref : submesh_refs)
123 auto const& submesh = submesh_ref.get();
124 auto const& bulk_element_ids =
125 *submesh.getProperties().getPropertyVector<std::size_t>(
128 if (bulk_element_ids.size() != submesh.getNumberOfElements())
131 "There is something terribly wrong with the mesh '{}'. The "
132 "size of 'bulk_element_ids' does not equal the number of "
133 "elements in the mesh: {} != {}",
134 submesh.getName(), bulk_element_ids.size(),
135 submesh.getNumberOfElements());
138 for (
auto const bulk_element_id : bulk_element_ids)
142 [[unlikely]]
if (bulk_element_id >= n_elements_bulk)
145 "Saw bulk element id {} in submesh '{}', but the bulk mesh "
146 "('{}') has only {} elements, i.e., the maximum allowed "
147 "bulk element id is {}.",
148 bulk_element_id, submesh.getName(), bulk_mesh.
getName(),
149 n_elements_bulk, n_elements_bulk - 1);
152 [[unlikely]]
if (bulk_element_covered[bulk_element_id])
155 "The bulk element id {} has already been covered by "
156 "another submesh. The second submesh covering this bulk "
158 bulk_element_id, submesh.getName());
161 bulk_element_covered[bulk_element_id] =
true;
165 return bulk_element_covered;
170 std::vector<std::reference_wrapper<MeshLib::Mesh>>
const& submesh_refs)
176 std::vector<bool>
const bulk_element_covered =
179 auto const n_elements_covered =
180 std::accumulate(bulk_element_covered.begin(),
181 bulk_element_covered.end(), std::size_t{0});
183 if (n_elements_covered == n_elements_bulk)
189 auto const non_covered_it = std::find(bulk_element_covered.begin(),
190 bulk_element_covered.end(),
false);
191 auto const non_covered_index =
192 std::distance(bulk_element_covered.begin(), non_covered_it);
195 "The bulk mesh ('{}') is not covered completely by the given "
196 "submeshes. Only {} out of {} elements are covered. The first element "
197 "that is not covered is #{}.",
198 bulk_mesh.
getName(), n_elements_covered, n_elements_bulk,
207 std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes)
211 if (oc.mesh_names_for_output.empty())
214 "You did not specify any meshes for submesh residuum output.");
217 if (!areElementsUnique(oc.mesh_names_for_output))
219 OGS_FATAL(
"The mesh names for submesh residuum output are not unique.");
222 auto const meshes_filtered =
223 filterMeshesForResiduumOutput(meshes, oc.mesh_names_for_output);
225 for (
auto const& mesh : meshes_filtered)
227 checkBulkIDMappingsPresent(mesh.get());
230 auto const& bulk_mesh =
232 checkNonOverlappingCover(bulk_mesh, meshes_filtered);
234 return {
createOutput(std::move(oc), output_directory, meshes),
235 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)