OGS
IdentifySubdomainMesh.cpp
Go to the documentation of this file.
1
10#include <range/v3/range/conversion.hpp>
11#include <unordered_map>
12#include <vector>
13
14#include "BaseLib/RunTime.h"
16#include "MeshLib/Mesh.h"
17#include "MeshLib/Node.h"
19#include "MeshNodeSearcher.h"
20
21namespace
22{
25std::vector<std::size_t> identifySubdomainMeshNodes(
26 MeshLib::Mesh const& subdomain_mesh,
27 MeshGeoToolsLib::MeshNodeSearcher const& mesh_node_searcher)
28{
29 // Convert nodes pointers needed for the mesh_node_searcher algorithm.
30 auto const& nodes = subdomain_mesh.getNodes();
31 std::vector<MathLib::Point3dWithID*> subdomain_points{begin(nodes),
32 end(nodes)};
33
34 auto const& bulk_node_ids =
35 mesh_node_searcher.getMeshNodeIDs(subdomain_points);
36
37 if (bulk_node_ids.size() != subdomain_mesh.getNumberOfNodes())
38 {
40 "Expected to find exactly one node in the bulk mesh for each node "
41 "of the subdomain; Found {:d} nodes in the bulk mesh out of {:d} "
42 "nodes in the subdomain.",
43 bulk_node_ids.size(), subdomain_mesh.getNumberOfNodes());
44 }
45
46 return bulk_node_ids;
47}
48
52std::vector<std::size_t> findElementsInMesh(
53 std::vector<std::size_t> const& node_ids,
54 std::vector<std::vector<std::size_t>> const& connected_element_ids_per_node)
55{
56 //
57 // Count how often an element is shared by all nodes.
58 //
59 std::unordered_map<std::size_t, int> element_counts(8);
60 for (auto const node_id : node_ids)
61 {
62 for (auto const element_id : connected_element_ids_per_node[node_id])
63 {
64 element_counts[element_id]++;
65 }
66 }
67
68 //
69 // Elements which are shared by as many nodes as the input nodes are the
70 // desired elements.
71 //
72 auto const nnodes = node_ids.size();
73 std::vector<std::size_t> element_ids;
74 for (auto const& pair : element_counts)
75 {
76 if (pair.second == static_cast<int>(nnodes))
77 {
78 element_ids.push_back(pair.first);
79 }
80 }
81
82 return element_ids;
83}
84
88std::vector<std::vector<std::size_t>> identifySubdomainMeshElements(
89 MeshLib::Mesh const& subdomain_mesh, MeshLib::Mesh const& bulk_mesh)
90{
91 auto const& bulk_node_ids = *MeshLib::bulkNodeIDs(subdomain_mesh);
92
93 // Allocate space for all elements for random insertion.
94 std::vector<std::vector<std::size_t>> bulk_element_ids_map(
95 subdomain_mesh.getNumberOfElements());
96
97 // For each node a vector of connected element ids of that node.
98 std::vector<std::vector<std::size_t>> connected_element_ids_per_node(
99 bulk_mesh.getNumberOfNodes());
100 for (auto const node_id : bulk_mesh.getNodes() | MeshLib::views::ids)
101 {
102 connected_element_ids_per_node[node_id] =
103 bulk_mesh.getElementsConnectedToNode(node_id) |
104 MeshLib::views::ids | ranges::to<std::vector>;
105 }
106 for (auto* const e : subdomain_mesh.getElements())
107 {
108 std::vector<std::size_t> element_node_ids(e->getNumberOfBaseNodes());
109 for (unsigned n = 0; n < e->getNumberOfBaseNodes(); ++n)
110 {
111 element_node_ids[n] = MeshLib::getNodeIndex(*e, n);
112 }
113 std::vector<std::size_t> element_node_ids_bulk(
114 e->getNumberOfBaseNodes());
115 std::transform(begin(element_node_ids), end(element_node_ids),
116 begin(element_node_ids_bulk),
117 [&bulk_node_ids](std::size_t const id)
118 { return bulk_node_ids[id]; });
119
120 std::vector<std::size_t> bulk_element_ids = findElementsInMesh(
121 element_node_ids_bulk, connected_element_ids_per_node);
122
123 if (bulk_element_ids.empty())
124 {
125 ERR("No element could be found for the subdomain element {:d}. "
126 "Corresponding bulk mesh node ids are:",
127 e->getID());
128 for (auto const i : element_node_ids_bulk)
129 {
130 ERR("\t{:d}", i);
131 }
132 OGS_FATAL(
133 "Expect at least one element to be found in the bulk mesh.");
134 }
135
136 bulk_element_ids_map[e->getID()] = std::move(bulk_element_ids);
137 }
138
139 return bulk_element_ids_map;
140}
141
144 MeshLib::Mesh& mesh, std::string_view property_name,
145 std::vector<std::size_t> const& values,
146 MeshLib::MeshItemType const mesh_item_type, bool const force_overwrite)
147{
148 auto& properties = mesh.getProperties();
149 if (!properties.existsPropertyVector<std::size_t>(property_name))
150 {
151 addPropertyToMesh(mesh, property_name, mesh_item_type, 1, values);
152 return;
153 }
154
155 //
156 // Check the existing property against new values.
157 //
158 auto& original_property =
159 *properties.getPropertyVector<std::size_t>(property_name);
160 if (std::equal(begin(original_property), end(original_property),
161 begin(values), end(values)))
162 {
163 INFO(
164 "There is already a '{:s}' property present in the subdomain mesh "
165 "'{:s}' and it is equal to the newly computed values.",
166 property_name, mesh.getName());
167 return;
168 }
169
170 //
171 // Property differs. Notify and update if forced.
172 //
173 WARN(
174 "There is already a '{:s}' property present in the subdomain mesh "
175 "'{:s}' and it is not equal to the newly computed values.",
176 property_name,
177 mesh.getName());
178
179 if (!force_overwrite)
180 {
181 OGS_FATAL("The force overwrite flag was not specified, exiting.");
182 }
183
184 INFO("Overwriting '{:s}' property.", property_name);
185 original_property.resize(values.size());
186 std::copy(begin(values), end(values), begin(original_property));
187}
188} // namespace
189
190namespace MeshGeoToolsLib
191{
193 MeshLib::Mesh const& bulk_mesh,
194 MeshNodeSearcher const& mesh_node_searcher,
195 bool const force_overwrite = false)
196{
197 BaseLib::RunTime time;
198 time.start();
199 auto const& bulk_node_ids =
200 identifySubdomainMeshNodes(subdomain_mesh, mesh_node_searcher);
201 INFO("identifySubdomainMesh(): identifySubdomainMeshNodes took {:g} s",
202 time.elapsed());
203
204 updateOrCheckExistingSubdomainProperty(
206 bulk_node_ids, MeshLib::MeshItemType::Node, force_overwrite);
207
208 time.start();
209 auto const& bulk_element_ids =
210 identifySubdomainMeshElements(subdomain_mesh, bulk_mesh);
211 INFO("identifySubdomainMesh(): identifySubdomainMeshElements took {:g} s",
212 time.elapsed());
213
214 // The bulk_element_ids could be of two types: one element per entry---this
215 // is the expected case for the boundary meshes; multiple elements per
216 // entry---this happens if the subdomain mesh lies inside the bulk mesh and
217 // has lower dimension.
218 // First find out the type, then add/check the CellData or FieldData.
219 if (all_of(begin(bulk_element_ids), end(bulk_element_ids),
220 [](std::vector<std::size_t> const& v) { return v.size() == 1; }))
221 {
222 // All vectors are of size 1, so the data can be flattened and
223 // stored in CellData or compared to existing CellData.
224 std::vector<std::size_t> unique_bulk_element_ids;
225 unique_bulk_element_ids.reserve(bulk_element_ids.size());
226 transform(begin(bulk_element_ids), end(bulk_element_ids),
227 back_inserter(unique_bulk_element_ids),
228 [](std::vector<std::size_t> const& v) { return v[0]; });
229
230 updateOrCheckExistingSubdomainProperty(
231 subdomain_mesh,
233 unique_bulk_element_ids, MeshLib::MeshItemType::Cell,
234 force_overwrite);
235 }
236 else
237 {
238 // Some of the boundary elements are connected to multiple bulk
239 // elements; Store the array in FieldData with additional CellData array
240 // for the number of elements, which also provides the offsets.
241 std::vector<std::size_t> flat_bulk_element_ids;
242 flat_bulk_element_ids.reserve(2 * bulk_element_ids.size()); // Guess.
243 std::vector<std::size_t> number_of_bulk_element_ids;
244 number_of_bulk_element_ids.reserve(bulk_element_ids.size());
245
246 for (std::vector<std::size_t> const& v : bulk_element_ids)
247 {
248 number_of_bulk_element_ids.push_back(v.size());
249 flat_bulk_element_ids.insert(end(flat_bulk_element_ids), begin(v),
250 end(v));
251 }
252
253 updateOrCheckExistingSubdomainProperty(
254 subdomain_mesh, "number_bulk_elements", number_of_bulk_element_ids,
255 MeshLib::MeshItemType::Cell, force_overwrite);
256 updateOrCheckExistingSubdomainProperty(
257 subdomain_mesh,
259 flat_bulk_element_ids, MeshLib::MeshItemType::IntegrationPoint,
260 force_overwrite);
261 }
262}
263} // namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
Definition of the Node class.
Definition of the RunTime class.
Count the running time.
Definition RunTime.h:29
double elapsed() const
Get the elapsed time in seconds.
Definition RunTime.h:42
void start()
Start the timer.
Definition RunTime.h:32
std::vector< std::size_t > getMeshNodeIDs(GeoLib::GeoObject const &geoObj) const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
Properties & getProperties()
Definition Mesh.h:134
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
PropertyVector< T > const * getPropertyVector(std::string_view name) const
void identifySubdomainMesh(MeshLib::Mesh &subdomain_mesh, MeshLib::Mesh const &bulk_mesh, MeshNodeSearcher const &mesh_node_searcher, bool const force_overwrite=false)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:225
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:188
MeshItemType
Definition Location.h:21
std::size_t getNodeIndex(Element const &element, unsigned const idx)
Definition Element.cpp:219
PropertyVector< std::size_t > const * bulkNodeIDs(Mesh const &mesh)
Definition Mesh.cpp:292
std::vector< std::size_t > identifySubdomainMeshNodes(MeshLib::Mesh const &subdomain_mesh, MeshGeoToolsLib::MeshNodeSearcher const &mesh_node_searcher)
void updateOrCheckExistingSubdomainProperty(MeshLib::Mesh &mesh, std::string_view property_name, std::vector< std::size_t > const &values, MeshLib::MeshItemType const mesh_item_type, bool const force_overwrite)
Updates or checks the existing mesh's property with the given values.
std::vector< std::vector< std::size_t > > identifySubdomainMeshElements(MeshLib::Mesh const &subdomain_mesh, MeshLib::Mesh const &bulk_mesh)
std::vector< std::size_t > findElementsInMesh(std::vector< std::size_t > const &node_ids, std::vector< std::vector< std::size_t > > const &connected_element_ids_per_node)